Word 自动化仅适用于管理员,或者在创建 word.application 后会出现延迟

发布于 2024-09-28 17:56:25 字数 569 浏览 1 评论 0原文

我们有一个用 Borland Delphi 编写的程序,它使用 Word 自动化来创建文档。 在安装(终端服务器)上,我们只能以本地管理员身份运行时才能使 Word 自动化工作。
当以另一个用户身份运行时,我们收到一条错误消息“Opdracht Mislukt -2146824090”(其荷兰语版本的 Office),我猜它被翻译为“操作失败”或“命令失败”。

用户对程序尝试放置新文档的文件夹具有读/写访问权限。

Office 2010
64 位 Windows Server 2008 R2 标准

该应用程序是 32 位 Windows 应用程序。

如果我在创建 word.application 后添加延迟(500ms),则一切正常。

WordApp   := CreateOleObject('Word.Application');
sleep(500);
Doc := WordApp.documents.Open(sFile,EmptyParam,true);

有人知道为什么 CreateOleObject 命令现在在 Word 应用程序可以使用之前返回吗?

We have a program made in Borland Delphi that uses Word automation to create documents.
On an installation (terminal server) we are only able to get the Word automation to work when running as local administrator.
When runnnig as anoter user we get an error message "Opdracht mislukt -2146824090" (its dutch version of Office), wich I guess is translated to "Operation failed" or "Command failed".

The user has read/write access to the folder where the program try to put the new document.

Office 2010
64bits Windows server 2008 R2 standard

The applicaion is 32bit windows application.

If I add a delay (500ms) after the word.application is created, everything works as normall.

WordApp   := CreateOleObject('Word.Application');
sleep(500);
Doc := WordApp.documents.Open(sFile,EmptyParam,true);

Anybody knows why the CreateOleObject command now returns before the Word application can be used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

浮光之海 2024-10-05 17:56:25

如果您想跟踪这一点,您可以使用 ProcessMonitor 之类的工具来跟踪 Word 自动化执行,直到您可以使用该应用程序。

似乎正在进行某种权限检查 - 但仅仅半秒似乎时间太多了。

If you want to track out that, you could use a tool like ProcessMonitor to trace the Word automation executions till the point which you can use the app.

Seems some kind of rights check is taking place - but half a second seems too much time just for this.

吃颗糖壮壮胆 2024-10-05 17:56:25

您可以尝试打开文档几次,或者Word在出现错误后完全无法运行?

WordApp := CreateOleObject('Word.Application');

while True do
begin
  try
    Doc := WordApp.documents.Open(sFile,EmptyParam,true);
    Break;
  except
    on E: EOleSysError do
    begin
      // raise error if it's not the expected "Command failed" error
      if E.ErrorCode <> -2146824090 then
        raise;
    end;
  end;
end;

编辑:

请参阅我的回答它提供了更好的解决方案并解释了为什么会发生这种情况。

You could try to open the Document a few times, or is Word totally borked after it gave the error?

WordApp := CreateOleObject('Word.Application');

while True do
begin
  try
    Doc := WordApp.documents.Open(sFile,EmptyParam,true);
    Break;
  except
    on E: EOleSysError do
    begin
      // raise error if it's not the expected "Command failed" error
      if E.ErrorCode <> -2146824090 then
        raise;
    end;
  end;
end;

Edit:

Please see my answer here which provides a better solution and an explanation why this happens.

巷雨优美回忆 2024-10-05 17:56:25

管理员帐户工作没有延迟,似乎没有任何权限可以执行任何操作,但 Word 恰好使用此帐户启动比普通域用户帐户快得多。

我可以接受延迟解决方法,但如果有人知道更好的方法,请告诉我。

The administrator account working wihtout delay, seems not to have anything with rights to do, but that Word happens to start much faster with this account than the normal domain user accounts.

I can live with the delay workaround, but if anyone knows a better way please let me know.

不喜欢何必死缠烂打 2024-10-05 17:56:25

我意识到这个线程已经很老了,但是我通过确保在退出之前关闭文档(oleDocument.Close)解决了这个问题。通过这样做,不需要任何类型的延迟等。请参阅下面的 Delphi 代码片段。

例子:

  oleWord     := Unassigned;
  oleDocument := Unassigned;

  Screen.Cursor := crHourGlass;

  try
    oleWord               := CreateOleObject('Word.Application');
    oleWord.Visible       := False;
    oleWord.DisplayAlerts := False;

    oleDocument := oleWord.Documents.Open(Worklist.Filename);
    oleDocument.SaveAs(Worklist.Filename, wdFormatDOSTextLineBreaks);

    oleDocument.Close;
    oleWord.Quit(False);
  finally
    oleDocument := Unassigned;
    oleWord     := Unassigned;

    Screen.Cursor := crDefault;
  end;

I realize this thread is quite old, but I solved this issue by making sure to close the document before quitting (oleDocument.Close). By doing so there is no need for any type of delays, etc. See Delphi code snippet below.

Example:

  oleWord     := Unassigned;
  oleDocument := Unassigned;

  Screen.Cursor := crHourGlass;

  try
    oleWord               := CreateOleObject('Word.Application');
    oleWord.Visible       := False;
    oleWord.DisplayAlerts := False;

    oleDocument := oleWord.Documents.Open(Worklist.Filename);
    oleDocument.SaveAs(Worklist.Filename, wdFormatDOSTextLineBreaks);

    oleDocument.Close;
    oleWord.Quit(False);
  finally
    oleDocument := Unassigned;
    oleWord     := Unassigned;

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