walle Posted April 29, 2008 Posted April 29, 2008 I'm trying to make an easy little script that saves all open Word docs. The problem lays in the attachment. It attaches to the first window on the first loop, saves it, and then closes it. But it wont attach to the second open Word docs on the second round. --> Word.au3 Warning from function _WordAttach, $_WordStatus_NoMatch --> Word.au3 Error from function _WordDocSave, $_WordStatus_InvalidDataType Why? #include <Word.au3> AutoItSetOption("WinTitleMatchMode", 4) $i = 0 Do $oDoc = "" $handle = WinGetHandle("classname=OpusApp", "") $oWordApp = _WordAttach ($handle, "HWND") If Not @error Then $oDoc = _WordDocGetCollection ($oWordApp, 0) EndIf _WordDocSave($oDoc) If @error = 1 Then ; Check if savepath is selected. _WordDocSaveAs ($oDoc, @ScriptDir & "\Test" & $i & ".doc") EndIf WinClose($handle) $i = $i + 1 sleep(500) Until WinGetHandle("classname=OpusApp", "") = ""
weaponx Posted April 29, 2008 Posted April 29, 2008 (edited) You should probably be using _WordDocClose instead of WinClose, also look at _WordQuit since you are reusing the object. Edited April 29, 2008 by weaponx
walle Posted April 29, 2008 Author Posted April 29, 2008 (edited) You should probably be using _WordDocClose instead of WinClose, also look at _WordQuit since you are reusing the object.Sure, but the _WordQuit closes all Word windows, not just the attached one, bugged sort of. Edited April 29, 2008 by walle
walle Posted April 29, 2008 Author Posted April 29, 2008 Can't figure it out, is there another way to approach my goal? Save all open Word docs.
PsaltyDS Posted April 29, 2008 Posted April 29, 2008 See if this is closer. Should be able to handle multiple instances of the Word app running, and multiple DOCs open in each instance: #include <Word.au3> AutoItSetOption("WinTitleMatchMode", 4) $i = 0 While 1 ; For each Word window $oDoc = "" $oWordApp = "" $handle = WinGetHandle("classname=OpusApp", "") If $handle = "" Then ExitLoop $oWordApp = _WordAttach($handle, "HWND") If Not @error Then ; For each DOC $colDocs = _WordDocGetCollection($oWordApp, -1) If @error = 0 Then For $oDoc In $colDocs _WordDocSave($oDoc) If @error = 1 Then ; Check if savepath is selected. _WordDocSaveAs($oDoc, @ScriptDir & "\Test" & $i & ".doc") $i += 1 EndIf Next Else MsgBox(16, "Error", "Error retreiving collection of Docs") ExitLoop EndIf Else MsgBox(16, "Error", "Error attaching to Word window by HWND") ExitLoop EndIf _WordQuit($oWordApp) WEnd Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
walle Posted April 30, 2008 Author Posted April 30, 2008 See if this is closer. Should be able to handle multiple instances of the Word app running, and multiple DOCs open in each instance: #include <Word.au3> AutoItSetOption("WinTitleMatchMode", 4) $i = 0 While 1 ; For each Word window $oDoc = "" $oWordApp = "" $handle = WinGetHandle("classname=OpusApp", "") If $handle = "" Then ExitLoop $oWordApp = _WordAttach($handle, "HWND") If Not @error Then ; For each DOC $colDocs = _WordDocGetCollection($oWordApp, -1) If @error = 0 Then For $oDoc In $colDocs _WordDocSave($oDoc) If @error = 1 Then ; Check if savepath is selected. _WordDocSaveAs($oDoc, @ScriptDir & "\Test" & $i & ".doc") $i += 1 EndIf Next Else MsgBox(16, "Error", "Error retreiving collection of Docs") ExitLoop EndIf Else MsgBox(16, "Error", "Error attaching to Word window by HWND") ExitLoop EndIf _WordQuit($oWordApp) WEnd Thanks PsaltyDs!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now