此方法或属性不可用,因为文档窗口未处于活动状态 [英] This method or property is not available because a document window is not active

查看:69
本文介绍了此方法或属性不可用,因为文档窗口未处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向文档添加内容控件时遇到问题.我需要将指定范围内的richtextcontentcontrol 添加到文档中的段落,但不需要添加到activedocument,以添加到我从路径打开的文档.

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 是一个 Microsoft.Office.Tools.Word.Document 而段落是一个 Microsoft.Office.Interop.Word.Paragraph此异常与 AddRichTextContentControl 相关所有错误是:

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):此方法或属性不可用,因为文档窗口未处于活动状态.

System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because a document window is not active.

推荐答案

只有一个可能有用的提示,因为我目前面临相同的错误描述,使用 Document.PrintOut () 打印打开的文档不可见.我认为该错误可能与您的情况相同.

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.

我目前在使用 Office 2003 和 2013 的 win32 上使用非常旧的 OLE/COM 代码.我想更改该代码,因为它在打开文档时显示 Word 窗口,而在我们的自动化启动时 Word 已经可见.代码通常可以运行多年,但如果文档在可见的 Word 窗口中打开,我们会遇到使用 COM 进行搜索+替换的性能问题.

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.

只有当不存在可能以前可见的窗口的 Word 进程存在时,我才会遇到错误,并且我以可见 = 假打开文档.如果我首先使用 .OpenOld () 通过 OLE/COM 初始化 Word 进程,或者只需从例如打开文档即可看到 Word资源管理器,我没有收到那个错误.

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.

我使用 .OpenOld()(短时间),因为我们的代码直到现在都使用 Word.Application.8,而 Word 97 中的 .Open() 似乎是 Word 2003 中的 .OpenOld().我不期望我们的客户执行 .OpenOld().但永远不知道,不想打破它.所以这只是一个后备.尽管如此,这种变化还是给了我错误.

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)别的__oIWDoc := oIWordDocuments:OpenOld( __oFS:fullPath)结束

IF __oWord:IWordApplication:visible __oIWDoc := oIWordDocuments:Open( __oFS:fullPath,,,,,,,,,,, lNewDocVisible) ELSE __oIWDoc := oIWordDocuments:OpenOld( __oFS:fullPath) ENDIF

我的下一步是设置在 .PrintOut () 之前可见的文档窗口.Word 可能已被使用,可能在打开文档和打印文档之间被用户关闭.但即使这样做,我也不认为这是一种值得信赖的方式.目前我想知道,如何安全地打开和打印不可见的文档.如果您因几乎相同的原因收到该错误,请分享您的结果:)

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

这篇关于此方法或属性不可用,因为文档窗口未处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆