开发者

Word automation does only work for administrator, or with a delay after creating word.application

开发者 https://www.devze.com 2023-01-22 04:55 出处:网络
We have a program made in Borland Delphi that uses Word automat开发者_如何学Goion to create documents.

We have a program made in Borland Delphi that uses Word automat开发者_如何学Goion 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?


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.


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.


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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消