[英]This method or property is not available because a document window is not active
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.
我需要将指定范围内的richtextcontentcontrol添加到文档中的段落,但不添加到activedocument中,以从路径中打开文档。
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: document是
Microsoft.Office.Tools.Word.Document
,段落是Microsoft.Office.Interop.Word.Paragraph
此异常与AddRichTextContentControl
一起AddRichTextContentControl
所有错误中:
System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because a document window is not active.
System.Runtime.InteropServices.COMException(0x800A11FD):此方法或属性不可用,因为文档窗口处于不活动状态。
have a probably helpfull hint only, as i face the same error description currently, printing a document opened invisible using Document.PrintOut (). 仅具有一个可能有用的提示,因为当前我面临相同的错误描述,所以使用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. 我目前在Office 2003和2013的win32上使用非常老的OLE / COM代码。 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.
我想更改该代码,因为打开文档时它会显示Word窗口,而自动化开始时Word已经可见。 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.
该代码通常可以使用多年,但是如果在可见的Word窗口中打开文档,则使用COM进行搜索+替换会出现性能问题。
I face the error only when no Word process with a probably former visible window exists and i open the document with visible=false. 仅当不存在可能带有以前可见窗口的Word进程并且我用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 eg explorer, i don't get that error.
如果我首先通过OLE / COM使用.OpenOld()初始化Word进程,或者仅通过从例如Explorer打开文档就可以看到Word,那么我不会得到该错误。
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 (). 我使用.OpenOld()(短时间),因为到目前为止我们的代码都使用Word.Application.8,而Word 97中的.Open()似乎在Word 2003中是.OpenOld()。我不希望我们的客户执行.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 IF __oWord:IWordApplication:可见__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 (). 对我而言,下一步是将文档窗口设置为在.PrintOut()之前可见。 Word might have been used, probably closed by the user between opening the document and printing it.
Word可能已被使用,可能是用户在打开文档和打印文档之间关闭的。 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 希望对大家有所帮助,非常感谢,汤姆·霍斯特曼(Tom Horstmann)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.