Sign in to follow this  
Followers 0
NutCracker

Save word files without quitting word

5 posts in this topic

Hi guys am stuck up at a point, when i save and close a word file all the word files opened get closed.

below is the code :

$oWordApp = _WordCreate ($wordfile, 1,0)

$oDoc = _WordDocGetCollection ($oWordApp, 0)

$oRange = $oDoc.Range

_WordDocFindReplace($oDoc, $sSearch, "", 0, $oRange)

With $oRange

; Insert a new line, updating the range.

.insertAfter (@CR)

; Collapse the range to the end point

.Collapse ($wdCollapseEnd)

EndWith

$oShape =_WordDocAddPicture ($oDoc, $file, 0, 1,$oRange)

If Not @error Then $oShape.Range.InsertAfter(@CRLF)

_WordDocSave ( $oWordApp )

_WordQuit($oWordApp, 0, 1, 0)

When i close the word file all other files get automatically closed.

What i want is only the created word file should get saved and closed whereas other opened word docs should be untouched. please help me out :unsure:

Share this post


Link to post
Share on other sites



Dont use _WordQuit , use _WordDocClose instead.

Share this post


Link to post
Share on other sites

thanks Juvigy but still not getting what i wanted other word files also get closed:

If Not @error Then $oShape.Range.InsertAfter(@CRLF)

;_WordDocSave ( $oDoc )

_WordDocClose ($oDoc,-1)

;_WordQuit($oWordApp, 0, 1, 0)

Share this post


Link to post
Share on other sites

Check this out:

$oWordApp = _WordAttach ("C:\2010.doc")

For $element in _WordDocGetCollection ($oWordApp)
    MsgBox(64, "Document FileName", $element .FullName)
    _WordDocClose ($element)
    Exit
Next

This code will only close 1(first in the collection) of the documents and leave word open.

Share this post


Link to post
Share on other sites

Hi all,

I had the same problem reported by @shreyash and finally I find this solution that seems to work fine:

#include <Word.au3>

$myDoc = @ScriptDir & "\Test.doc"
$oWordApp = ObjCreate("Word.Application") 
_WordDocOpen($oWordApp, $myDoc)
$oDoc = _WordDocGetCollection ($oWordApp, 0) ; 0 = Returns the Active Document

; work with $oDoc, for instance...
$oDoc.Range.insertAfter("Hello world!" & @CR)

_WordDocSave($oDoc)

For $element in _WordDocGetCollection ($oWordApp)
   MsgBox(64, "Document FileName", $element .FullName)
   _WordDocClose($element)
   ExitLoop
Next
_WordQuit ($oWordApp)
ShellExecute($myDoc)

...and the other opened word docs remain untouched! :)

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