问题
I have a problem with adding content control's to document. I need add richtextcontentcontrol in specified range to paragraph in document, but no to activedocument, to document witch i open from path.
Word.Range range = paragraph.Range;
range.SetRange(1, 3);
try
{
var richTextControl2 = document.Controls.AddRichTextContentControl(range, "richTextControl" + counter);
richTextControl2.PlaceholderText = "Enter your first name";
}
catch(COMException e)
{
Debug.WriteLine("Error :" + e);
}
document is a Microsoft.Office.Tools.Word.Document
and paragraph is a Microsoft.Office.Interop.Word.Paragraph
This exception is on the line with AddRichTextContentControl
All error is:
System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because a document window is not active.
回答1:
have a probably helpfull hint only, as i face the same error description currently, printing a document opened invisible using Document.PrintOut (). I think the error might have the same reason in your case.
I work with very old OLE/COM code on win32 with Office 2003 and 2013 currently. I want to change that code, as it displays a Word window when opening a document while Word was already visible when our automation starts. The code in general works for years but we have a performance issue with search+replace using COM if the document is opened in a visible Word window.
I face the error only when no Word process with a probably former visible window exists and i open the document with visible=false. If i initialize a Word process by OLE/COM at first with .OpenOld () or Word is visible by just openning a document from e.g. explorer, i don't get that error.
I use .OpenOld () (for a short time), because our code uses Word.Application.8 till now and .Open () in Word 97 seems to be .OpenOld () in Word 2003. I don't expect our customers executing .OpenOld (). But never know, don't want to break it. So it's just a fallback. Still, that change gives me the error.
This code solves the error for me somehow:
IF __oWord:IWordApplication:visible __oIWDoc := oIWordDocuments:Open( __oFS:fullPath,,,,,,,,,,, lNewDocVisible) ELSE __oIWDoc := oIWordDocuments:OpenOld( __oFS:fullPath) ENDIF
Next step for me is setting the documents window visible before .PrintOut (). Word might have been used, probably closed by the user between opening the document and printing it. But even doing so, i don't think that a trustfull way. Currently i wonder, how to safely open and print a document invisible. Please share your results if you get that error by nearly same reason:)
Hope to help and many thanks, Tom Horstmann
来源:https://stackoverflow.com/questions/32786644/this-method-or-property-is-not-available-because-a-document-window-is-not-active