try {Try to check any existing word app} WordApp:= GetActiveOleObject('Word.Application'); {If word app is found , then make sure no documents were opened} if WordApp.Documents.Count > 0 then begin MessageDlg('Program detected that a Word instance is running in your system.' + #13#10 + 'In order to avoid data losing, please save and close your current document.', mtError, [mbOK, mbHelp], 0); WordApp:= Null; end; except on E: EOleSysError do {No Word Opened, then start word } begin try WordApp:= CreateOleObject('Word.Application'); {Open Word} except WordApp:= Null; end; end; end;
if VarType(WordApp) <> varNull then begin WordApp.Visible := False; Action(); end;