为什么我的 IMessageFilter 并不总是有效?
我正在研究 Word 自动化,并消除“呼叫被被呼叫者拒绝”/“消息过滤器指示应用程序正忙”错误,我实现了 IMessageFilter。当我直接自动化 Word 时,消息过滤器就像一个魅力:
Word.Documents.Open(...)
Document.SaveAs(...)
但是当我调用 TOleContainer.DoVerb(ovPrimary) 时,当 Word 显示模式对话框时,我仍然会收到错误。为什么 MessageFilter 不能与 TOleContainers DoVerb 方法一起使用?
I'm working on Word automation and to get rid of "Call was rejected by callee" / "the message filter indicated that the application is busy" errors I implemented an IMessageFilter. The messagefilter works like a charm when I automate Word directly like:
Word.Documents.Open(...)
Document.SaveAs(...)
But when I call TOleContainer.DoVerb(ovPrimary), I still get errors when Word is displaying a modal dialog. Why does the MessageFilter not work with TOleContainers DoVerb methode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当Word 处于交互状态(即显示对话框)时,您总是会收到“呼叫被被呼叫者拒绝”的信息。这并不限于Word。 Excel 也会发生这种情况,例如当用户编辑单元格时。而且它在用户界面中也不必很明显。当您开始编辑单元格、将焦点移至另一个应用程序并返回 Excel 时,UI 不会给您任何线索,但它仍处于“交互”模式,并且会拒绝自动化调用,并显示“调用被被调用者拒绝”错误。
因此,基本上,当您将 Word 与用户交互(而不仅仅是后台进程中的 Word)结合起来实现自动化时,您应该准备好获取并处理这些错误。
编辑
如果您想在调用任何其他 COM 方法之前知道 Excel 或 Word 是否处于交互模式:只需询问 COM 服务器是否“就绪”:
更新
显然,Word 没有“Ready”属性(谁说 Microsoft 是一致的?)。在这种情况下,您需要通过在实际调用之前调用一个简单(且快速)的属性来自行确定其准备情况,并假设当抛出异常时,Word 尚未准备好。在上面的示例中,Version 是在 Ready 属性之前检索的。如果抛出异常,我们就假设应用程序(本例中为 Excel)尚未准备好并相应地继续。
大致如下:
注意当对话框打开时,Word.Version不会抛出异常,因此这对于确定 Word 是否准备就绪没有用。 :( 你必须尝试找到一个可以做到的。
"Call was rejected by callee" is what you always get when Word is in interactive state, ie displaying a dialog. This is not restricted to Word. It also happens with Excel, for example when the user was editing a cell. And it does not have to be obvious in the user interface either. When you start editing a cell, move focus to another application and come back to Excel, the UI doesn't give you a clue but it is still in "interactive" mode and will reject automation calls with the "Call was rejected by callee" error.
So basically when you automate Word in conjunction with user interaction (and not just with Word in a background process), you should be prepared to get and handle these errors.
Edit
If you want to know whether Excel or Word is in interactive mode before calling any other COM method: just ask the COM-server whether it is "Ready":
Update
Apparently Word doesn't have a "Ready" property (whoever said Microsoft was consistent?). In that case you need to determine its readiness yourself by calling a simple (and fast) property before the actual call, and assuming that when that throws an exception, Word isn't ready. In the above example the Version is retrieved before the Ready property. If that throws an exception, we just assume that the application (Excel in this case) isn't ready and proceed accordingly.
Something along the lines of:
Note Word.Version does not throw an exception when a dialog is open, so that is no use for figuring out whether Word is ready. :( You will have to experiment to find one that does.
所有异常,例如,在某些时候,办公应用程序会“挂起”其对象模型,此时它无法被调用并抛出:
0x800AC472 (VBA_E_IGNORE)
IMessageFilter 并不处理 为此,您必须将调用放入循环中并等待其成功:
请参阅 此处了解更多详细信息。
IMessageFilter doesn't handle all exceptions, for example, at some points, office applications 'suspend' their object model, at which point it cannot be invoked and throws:
0x800AC472 (VBA_E_IGNORE)
In order to get around this, you have to put your call in a loop and wait for it to succeed:
See here for more details.