Issam
Issam

Reputation: 186

How to detect if MS word document has been closed

I have used the next code to open my document by using MS Word :

ShellExecute(Handle, nil,
  PChar(ExtractFilePath(Application.ExeName)+'Data\Quotation-Terms-1.rtf'),
  nil, nil, SW_SHOWNORMAL);

and i want to load my document inside my TRichEdit control after the user closed the MS Word document.

thank you.

Upvotes: 1

Views: 179

Answers (1)

fpiette
fpiette

Reputation: 12292

Try to pen the file as a TFileStream, passing fmShareExclusive as rights argument. If open is a success, then the file is not open in Word.

Success := FALSE;
try
    fs := TFileStream.Create(ExtractFilePath(Application.ExeName)+'Data\Quotation-Terms-1.rtf',
                             fmOpenReadWrite,
                             fmShareExclusive);
    Success := TRUE;
except
    on E:EFOpenError do
         Success := FALSE;
    else
         raise;
end;
if success then begin
    // More code
end;

It is possible that you may need to wait a few second before testing so that Word has time to load the file.

Upvotes: 1

Related Questions