Sign in to follow this  
Followers 0
saywell

Word Question

3 posts in this topic

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
EndIf

but 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



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

Thanks, MrMitchell.

That looks promising - will give it a try.

William

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  
Followers 0