Jump to content

It won't attach the second time.


 Share

Recommended Posts

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", "") = ""
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...