We're using the following to use Word as a reporting tool.  It seems to behave
flawlessly.

  try
    MSWord := GetActiveOleObject('word.application');
    WasRunning := True;
  except
    WasRunning := False;
    try
      MSWord := CreateOleObject('word.Application');
    except
      Errors.Add('Was Unable to Connect to MSWord');
      Exit;
    end;
  end;
  OrigState := MSWord.WindowState;
  MSWord.WindowState := 2;

Note that the Windowstate is to minimize the word application.
After this the report documents are created in another function with
  try
    if iTemplate='' then Result := MSWord.Documents.Add
    else Result := MSWord.Documents.Add(iTemplate,False);
    SetLength(DocArray,length(DocArray)+1);
    with DocArray[High(DocArray)] do begin
      {Some private stuff}
    end;
  except
    Errors.Add('Unable to create "'+iName+'"');
  end;

--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax
-----Original Message-----
From: John Mason <[EMAIL PROTECTED]>
To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
Date: Monday, 21 December 1998 10:40
Subject: [DUG]: Another OLE problem


I am writing an app to automate the creation of Excel and Word 
documents. I'm using OLE to create and save a new document, but 
because I want Word to remain open even if my app is closed 
Word needs to be running before I create the OLE object 
(otherwise Word is closed when my app is closed).

So, I'm using the following code to detect if Word is running and if 
not then to start it before creating the OLE object. The trouble is, I 
think,  that the CreateOLEObject command is issued before Word 
has time to completely start and is creating another instance of 
Word that is closed when the procedure is completed, leaving the 
instance of Word that I started by ShellExecute with a blank 
document.

Am I doing this the wrong way? The only solution I have thought of 
is to insert a 2 or 3 second delay before the CreateOLEObject 
statement, but I'm not sure how I can do this.

Any advice would be greatly appreciated.


{check if Word is running, if not then start it}
MyWnd:=FindWindow('OpusApp',nil);
if MyWnd<>0 then
  if IsIconic(MyWnd) then
    ShowWindow(MyWnd,SW_MAXIMIZE)
  else
     BringWindowToTop(MyWnd)
  else begin
    ShellExecute(Application.Mainform.handle,nil,' winword.exe','', 
chrWordDir,SW_SHOWNORMAL);
   end;

{ wait until Word is running, this is my ineffectual attempt to wait 
for Word to start}
while FindWindow('OpusApp',nil)=0 do begin
  {do nothing}       
end;


v:=CreateOLEObject('word.basic');
v.filenew(template:=strTemplate);
v.FileSaveAs(name:=strFilename+'.doc');
v.ToolsMacro(name:=strMacroName,run:=1,show:=0)



John Mason
Kingston Morrison
email: [EMAIL PROTECTED]

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to