Sign in to follow this  
Followers 0
Kohr

Save methods using Word.au3

5 posts in this topic

I am using the Microsoft Word Automation UDF Library for AutoIt3 (Word.au3) library to save .png files to a word document. The following code works great.

#include <Word.au3>
$oWordApp = _WordCreate ("", 0, 0)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
$sPath = "C:\Program Files\Temp\"
$file = "MWSnap.png"
$oShape = _WordDocAddPicture ($oDoc, $sPath & $file, 0, 1)
_WordDocSaveAs ($oDoc, @DesktopDir & "\WordOutput.doc")
_WordQuit ($oWordApp)

The following code will give console messages "--> Word.au3 Warning from function _WordDocOpen (The specified file does not exist, but we will attempt to create it.)" and "--> Word.au3 Info from function _WordDocOpen (The specified file was created successfully.)" when it is 1st run (without the file existing so I believe these messages are normal). The issue is that the picture is not in the word document.

All other times I run the code I do not receive any console errors and the picture never gets inserted. The only thing that is different is the way I am using _WordCreate and the function used to save the file. Any help would be appreciated.

#include <Word.au3>
$oWordApp = _WordCreate (@DesktopDir & "\WordOutput.doc", 0, 0)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
$sPath = "C:\Program Files\Temp\"
$file = "MWSnap.png"
$oShape = _WordDocAddPicture ($oDoc, $sPath & $file, 0, 1)
_WordDocSave ($oDoc)
_WordQuit ($oWordApp)

Kohr

Share this post


Link to post
Share on other sites



Much better explanation! Let me do some testing/troubleshooting and hopefully I'll have something for you later today.

Share this post


Link to post
Share on other sites

This wasnt too hard to figure out. The problem is with the way I initially create the file if it doesn't exist. It is created as a text file not a word document; this causes _WordDocSave to drop anything that is not plain text. A quick word around is to use _WordDocSaveAs function with only the first parameter, which will save it as a word document. The example below should solve the problem.

#include <Word.au3>
$oWordApp = _WordCreate (@DesktopDir & "\WordOutput.doc", 0, 0)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
$sPath = "C:\Program Files\Temp\"
$file = "MWSnap.png"
$oShape = _WordDocAddPicture ($oDoc, $sPath & $file, 0, 1)
_WordDocSaveAs ($oDoc)
_WordQuit ($oWordApp)

Share this post


Link to post
Share on other sites

No problem, glad I was able to help.

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