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, vForma开发者_StackOverflow社区t, 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 :-(
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
Instead of using TWordApplication, use CreateOLEObject:
var WordApp: Variant;
procedure OpenWordFIle( const Filename: String );
begin
WordApp := CreateOLEObject('Word.Application');
WordApp.Visible := False;
WordApp.Documents.Open( Filename );
Application.ProcessMessages;
end;
To close it gracefully:
procedure CloseWordFile;
begin
WordApp.ActiveDocument.Close( $00000000 );
WordApp.Quit;
WordApp := unassigned;
end;
If you don't close it, Word application will be open even after your close your Delphi application.
Some useful resources where you can find more options to open/close Word Files:
http://msdn.microsoft.com/en-us/library/office/ff835182.aspx
How can I call documents.open and avoid the 'file in use' dialog?
in my case it happend similar as you described. I looks the application is still running even if you disconnect. The first time it will not be shown, but as soon as you have a second open then the application will be visible. in My case it helped to explicitly quite the application. It quit's only the instance that is doing the work in background. Another open document edited by the local user will not be touched.
WordDocument.Disconnect;
**WordApplication.Quit;**
WordApplication.Disconnect;
精彩评论