Jump to content

Save methods using Word.au3


Kohr
 Share

Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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)
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...