Microsoft.Office.Interop.Word“无法激活应用程序”
当尝试通过 Office 互操作从我们的应用程序启动 Word 时,一些用户遇到了一个问题:
using Word = Microsoft.Office.Interop.Word;
public void ShowWord()
{
_word = new Word.ApplicationClass();
_word.Visible = true;
_word.Activate();
}
如果 Word 不总是打开,则会抛出 COM 异常,指出“无法激活应用程序”。在调用 _word.Activate()
之前添加 Thread.Sleep(1000)
可以防止这种情况发生,但显然并不理想。
public void ShowWord()
{
_word = new Word.ApplicationClass();
_word.Visible = true;
Thread.Sleep(1000)
_word.Activate();
}
有没有人以前见过这个并知道是什么原因造成的以及解决这个问题的正确方法是什么?
We are having a problem experienced by a few users when attempting to launch Word from our application via the office interop:
using Word = Microsoft.Office.Interop.Word;
public void ShowWord()
{
_word = new Word.ApplicationClass();
_word.Visible = true;
_word.Activate();
}
If word is not always open a COM exception is thrown stating "Cannot activate application." Adding a Thread.Sleep(1000)
before calling _word.Activate()
prevents this, but obviously is not ideal.
public void ShowWord()
{
_word = new Word.ApplicationClass();
_word.Visible = true;
Thread.Sleep(1000)
_word.Activate();
}
Has anyone seen this before and knows what is causing this and what the right way to fix this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们遇到了类似的问题,Word 似乎正在异步等待操作系统显示其窗口。我们解决这个问题的方法是等待 Visible 属性返回 true:
希望这对某人有帮助。
We encountered a similar issue, and it seems that Word is asynchonously waiting for the OS to show its window. The way we resolved this is by waiting until the Visible property returns true:
Hope this helps somebody.
您的应用程序是否有权激活Word COM对象?
检查
DCOMCNFG
本地激活安全要求是什么。但是,不确定为什么您的 Thread.Sleep(1000) 会允许它工作?
Does your application have permission to activate the Word COM object?
Check in
DCOMCNFG
what the local activation security requirements are.However, not sure why your
Thread.Sleep(1000)
would allow it to work?