saywell 3 Posted December 19, 2010 Hi,I'm [still] developing my program that I've posted about before, which organises word documents that the user creates from within my script.I've found that if the user has a word doc open before creating one within the script, it often causes errors.I'd like to try to stop this happening by instigating closure of the running instance(s) of Word.I can do this for documents created in autoit - eg as in the _WordDocGetCollection example script but I can't do it for previously-existing ones.I tried#include <Word.au3> $oWordApp = ProcessExists ("winword.exe") ; returns PID of process If $oWordApp <>0 Then $oDocuments = _WordDocGetCollection ($oWordApp) MsgBox(0, "Open Documents", "You already have " & @extended & " open Word document(s)." & @CR & "Click OK to close/save these and continue" & @CR & "or click cancel to return to CDM menu") ; @extended contains document count _WordQuit ($oWordApp) ; If created by AutoIT and doc added, this closes the lot. Prompts to save if required. WinActivate ("Microsoft Office Word") ; check dialogue box for earlier word versions EndIfbut this fails. Looking at the helpfile, it seems that the PID isn't what's needed, rather the Object variable of the Word.Application object (s).But I'm not sure how to fing this.Could anyone give me a pointer please?Regards,William Share this post Link to post Share on other sites
MrMitchell 16 Posted December 20, 2010 There are better ways to do it, but here goes...I couldn't figure out how to get two docs open in the same Word instance or process or whatever so I couldn't test everything. I just opened a bunch of windows and the following closed them all (don't save, don't prompt user) then closes the process(es) too. #include <Word.au3> Opt("WinTitleMatchMode", 2) Dim $window, $oWord, $oWordCollection, $docClosureStatus, $appClosureStatus While 1 $window = WinGetTitle("- Microsoft Word") If $window Then $oWord = _WordAttach($window, "Title") If IsObj($oWord) Then MsgBox(0, "Found", 'Attached to "' & $window & '" window.') $oWordCollection = _WordDocGetCollection($oWord) For $each In $oWordCollection $docClosureStatus = _WordDocClose($each, 0, 1, 0) If $docClosureStatus Then MsgBox(0, "Info", "Closed document.") Next $appClosureStatus = _WordQuit($oWord, 0, 1, 0) If $appClosureStatus Then MsgBox(0, "Info", "Closed app instance") Else ;No more windows (With "- Microsoft Word" in the title anyway) EndIf Else ;No windows found (With "- Microsoft Word" in the title) ExitLoop EndIf WEnd Exit Share this post Link to post Share on other sites
saywell 3 Posted December 20, 2010 Thanks, MrMitchell. That looks promising - will give it a try. William Share this post Link to post Share on other sites