Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Word invisible during OLE automation from Delphi

From our application we use OLE automation to build a fairly complex Word-document. I would like to make Word invisible while the document is being made, since there is a lot of pasting and insertions that takes quite a long time.

I use the following code to establish a Word connection:

function ConnectToWord : TWordAutomationResult;
begin
  WordApp := TWordApplication.Create(nil);

  try
    WordApp.Connect;
    WordApp.Visible := false; 
  except on E: Exception do
    begin
      Result := waeErrorConnectingToWord;
      exit;
    end;
  end;
end;      

And I use the following code to open an existing document, which is then edited by my application.

function TWordAUtomation.OpenDocument(aFileName: string) : WordDocument;
var vFileName,
    vConfirmConversions,
    vReadOnly,
    vAddToRecentFiles,
    vPasswordDocument,
    vPasswordTemplate,
    vRevert,
    vWritePasswordDocument,
    vWritePasswordTemplate,
    vFormat,
    vEncoding,
    vVisible,
    vOpenConflictDocument,
    vOpenAndRepair,
    vWdDocumentDirection,
    vNoEncodingDialog  : OleVariant;
begin
  Result := nil;
  if not FileExists(aFileName) then exit;

  vFileName                := aFileName;
  vConfirmConversions      := True;
  vReadOnly                := False;
  vAddToRecentFiles        := False;
  vPasswordDocument        := EmptyParam;
  vPasswordTemplate        := EmptyParam;
  vRevert                  := True; 
  vWritePasswordDocument   := EmptyParam;
  vWritePasswordTemplate   := EmptyParam;
  vFormat                  := wdOpenFormatAuto;
  vEncoding                := EmptyParam;
  vVisible                 := False; //Document should be invisible
  vOpenConflictDocument    := EmptyParam;
  vOpenAndRepair           := EmptyParam;
  vWdDocumentDirection     := EmptyParam;
  vNoEncodingDialog        := EmptyParam;

  Result := WordApp.Documents.Open(vFileName, vConfirmConversions, vReadOnly, vAddToRecentFiles, vPasswordDocument, vPasswordTemplate, vRevert, vWritePasswordDocument, vWritePasswordTemplate, vFormat, vEncoding, vVisible, vOpenAndRepair, vWdDocumentDirection, vNoEncodingDialog);
end;

It works on my computer! (TM)

For some of our customers Word remains visible during the editing process. What reasons can there be for this? As far as I can tell the problem arises for customers that use some sort of remote computing, like managed clients etc. Are there some additional properties that deals with application visibility that only have effect during remote desktop connections etc? I'm not very knowledgeable about such things :-(

like image 505
Svein Bringsli Avatar asked Oct 12 '10 08:10

Svein Bringsli


1 Answers

I'm maintaining the Word automation for our software and also had reports of Word windows popping up in Citrix clients. I don't know what causes this and how to get rid of it.

There is only one way I can simulate Word becoming visible again and that is opening a Word-document while your application is processing. But I don't think that is the cause of your problems.

PS: You call TWordApplication.Connect and then you set Visible to False. Know that when you call Connect and you haven't changed ConnectKind, it will connect to a running instance of Word. When your client is editing a document this document will suddenly dissappear. Perhaps it is better to set ConnectKind to NewInstance so you always work in a new winword.exe process. The existing winword.exe will remain available for your client and he can continue working at his document while your application is processing the other.

Ofcourse this approach has some drawbacks too:

  • When your client opens a new Word-document, it is opened in your instance of Word
  • You can get errors on Normal.dot being modified by another application
like image 134
The_Fox Avatar answered Sep 29 '22 00:09

The_Fox