Sign in to follow this  
Followers 0
AndyS01

_WordQuit() closes other MS Word windows

6 posts in this topic

I call _WordCreate() to create a new Word doc and when I call _WordQuit() later on, it closes my Word doc and any other word docs that were opened at that time.

Here's my test code:

#include <Word.au3>
    MsgBox(0, "INFO", "before _WordCreate()")
    $oWordApp = _WordCreate()
    MsgBox(0, "INFO", "after _WordCreate()")

    MsgBox(0, "INFO", "before _WordQuit()")
    _WordQuit($oWordApp, -1)
    MsgBox(0, "INFO", "after _WordQuit()")
    Exit (0)

To see the problem, open 1 or 2 Word docs, then run this test. When the _WordQuit() API is called, it closes the blank document as well as all Word docs that are currently opened.

Share this post


Link to post
Share on other sites



I call _WordCreate() to create a new Word doc and when I call _WordQuit() later on, it closes my Word doc and any other word docs that were opened at that time.

Here's my test code:

#include <Word.au3>
    MsgBox(0, "INFO", "before _WordCreate()")
    $oWordApp = _WordCreate()
    MsgBox(0, "INFO", "after _WordCreate()")

    MsgBox(0, "INFO", "before _WordQuit()")
    _WordQuit($oWordApp, -1)
    MsgBox(0, "INFO", "after _WordQuit()")
    Exit (0)

To see the problem, open 1 or 2 Word docs, then run this test. When the _WordQuit() API is called, it closes the blank document as well as all Word docs that are currently opened.

Look at _WordDocClose() in the help file.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Share this post


Link to post
Share on other sites

I changed my test code to do that and it still closes all Word docs when I run my test.

here's the new test code:

#include <Word.au3>

MsgBox(0, "INFO", "before _WordCreate()")

$oWordApp = _WordCreate()
If ($oWordApp == 0) Then myErrorExit(@error, "_WordCreate")

MsgBox(0, "INFO", "after _WordCreate()" & @CRLF & "before _WordDocOpen()")

$odoc = _WordDocOpen($oWordApp, "c:\temp\test2.doc", 0, 0, 1, 0)
If ($odoc == 0) Then myErrorExit(@error, "_WordDocOpen")

MsgBox(0, "INFO", "after _WordDocOpen()" & @CRLF & "before _WordDocClose()")

$res = _WordDocClose($odoc)
If ($res == 0) Then myErrorExit(@error, "_WordDocClose")

MsgBox(0, "INFO", "after _WordDocClose()" & @CRLF & "before _WordQuit()")

$res = _WordQuit($oWordApp, -1)
If ($res == 0) Then myErrorExit(@error, "_WordQuit")
Exit(0)

Func myErrorExit($err, $fname)
    MsgBox(0, "ERROR", "Error " & $err & " after calling " & $fname)
    Exit (1)
EndFunc

Please try this code to see the error yourself.

Andy

Share this post


Link to post
Share on other sites

I changed my test code to do that and it still closes all Word docs when I run my test.

here's the new test code:

#include <Word.au3>

MsgBox(0, "INFO", "before _WordCreate()")

$oWordApp = _WordCreate()
If ($oWordApp == 0) Then myErrorExit(@error, "_WordCreate")

MsgBox(0, "INFO", "after _WordCreate()" & @CRLF & "before _WordDocOpen()")

$odoc = _WordDocOpen($oWordApp, "c:\temp\test2.doc", 0, 0, 1, 0)
If ($odoc == 0) Then myErrorExit(@error, "_WordDocOpen")

MsgBox(0, "INFO", "after _WordDocOpen()" & @CRLF & "before _WordDocClose()")

$res = _WordDocClose($odoc)
If ($res == 0) Then myErrorExit(@error, "_WordDocClose")

MsgBox(0, "INFO", "after _WordDocClose()" & @CRLF & "before _WordQuit()")

$res = _WordQuit($oWordApp, -1)
If ($res == 0) Then myErrorExit(@error, "_WordQuit")
Exit(0)

Func myErrorExit($err, $fname)
    MsgBox(0, "ERROR", "Error " & $err & " after calling " & $fname)
    Exit (1)
EndFunc

Please try this code to see the error yourself.

Andy

I'm not sure if you meant to, but your code is a little odd. You open the document as read-only, but later when you run _WordQuit you are telling it to save changes.

The code below works for me. I changed the creation of the Word object and I also am NOT opening the file in read-only mode. See if this fits your needs...

#include <Word.au3>

MsgBox(0, "INFO", "before _WordCreate()")

$oWordApp = ObjCreate("Word.Application")
$oWordApp.visible=1
If ($oWordApp == 0) Then myErrorExit(@error, "_WordCreate")

MsgBox(0, "INFO", "after _WordCreate()" & @CRLF & "before _WordDocOpen()")

$odoc = _WordDocOpen($oWordApp, "c:\temp\test2.doc")
If ($odoc == 0) Then myErrorExit(@error, "_WordDocOpen")

MsgBox(0, "INFO", "after _WordDocOpen()" & @CRLF & "before _WordDocClose()")

$res = _WordDocClose($odoc)
If ($res == 0) Then myErrorExit(@error, "_WordDocClose")

MsgBox(0, "INFO", "after _WordDocClose()" & @CRLF & "before _WordQuit()")

$res = _WordQuit($oWordApp, -1)
If ($res == 0) Then myErrorExit(@error, "_WordQuit")
Exit(0)

Func myErrorExit($err, $fname)
    MsgBox(0, "ERROR", "Error " & $err & " after calling " & $fname)
    Exit (1)
EndFunc

Share this post


Link to post
Share on other sites

Look at _WordDocClose() in the help file.

In this instance the help file isn't very helpful. _WordDocClose() isn't the issue. The problem is with _WordQuit(). IF the Word object is created with the _WordCreate() function then a subsequent call to _WordQuit() will exit ALL instances of Word not just the one(s) created by autoit. IF you manually create the com object with ObjCreate() then _WordQuit() functions as you would expect and only exits instances of Word created by the object in autoit.

Share this post


Link to post
Share on other sites

You're right. Using the ObjCreate() instead of the _WordCreate works as I want it to.

BTW: The reason I was setting the object to read-only mode is that my actual application reads a specified Word file and extracts certain data from one of its tables. My code has to work even if the file I'm extracting from is already open in another window.

Thank you for the solution to this very perplexing problem.

Andy

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