Jump to content
Sign in to follow this  
walle

It won't attach the second time.

Recommended Posts

walle

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", "") = ""

Share this post


Link to post
Share on other sites
weaponx

You should probably be using _WordDocClose instead of WinClose, also look at _WordQuit since you are reusing the object.

Edited by weaponx

Share this post


Link to post
Share on other sites
walle

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 by walle

Share this post


Link to post
Share on other sites
walle

Can't figure it out, is there another way to approach my goal?

Save all open Word docs.

Share this post


Link to post
Share on other sites
PsaltyDS

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

:D


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

Share this post


Link to post
Share on other sites
walle

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

:D

Thanks PsaltyDs!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×