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