Microsoft.Office.Interop.Word“无法激活应用程序”

发布于 2024-10-25 12:49:04 字数 576 浏览 1 评论 0原文

当尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-11-01 12:49:06

我们遇到了类似的问题,Word 似乎正在异步等待操作系统显示其窗口。我们解决这个问题的方法是等待 Visible 属性返回 true:

public void ShowWord()
{
  _word = new Word.Application();
  _word.Visible = true;

  System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
  while (!_word.Visible && sw.ElapsedMilliseconds < 10000)
  { /* Just Wait!! (at most 10s) */}
  _word.Activate();
}

希望这对某人有帮助。

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:

public void ShowWord()
{
  _word = new Word.Application();
  _word.Visible = true;

  System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
  while (!_word.Visible && sw.ElapsedMilliseconds < 10000)
  { /* Just Wait!! (at most 10s) */}
  _word.Activate();
}

Hope this helps somebody.

葬心 2024-11-01 12:49:06

您的应用程序是否有权激活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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文