Vaerming Posted January 9, 2014 Posted January 9, 2014 Hi, I downloaded AutoIt yesterday and so far everything worked fine. However, today I ran into an issue with Word (German, 2010) which I cannot resolve even though I searched the forum (and elsewhere) for similar subjects. Short story: I create a new word document and try either to get a range or to save it and both operations fail. Trying to get a range fails somewhere in word.au and trying to save the document gives me a COM error (invalid object). Getting the range and e.g. inserting an image works if I open a new document with a DOC (without x) extension. Thanks a lot ; everything is fine up to this point Global $oDoc If $bFound = True Then ; document exists so open it $oDoc = _Word_DocOpen($oWord, "my_absolute_path\Test.doc", Default, Default, True) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", "my_absolute_path\Test.doc" & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Else ; document does not exist, create it _Word_DocAdd( $oWord ) $oDoc = _Word_DocGet( $oWord ) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Error accessing collection of documents." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; code below shows that $oDoc is an object If IsObj($oDoc) Then MsgBox($MB_SYSTEMMODAL, "", "The variable is an object") Else MsgBox($MB_SYSTEMMODAL, "", "The variable is not an object") EndIf ; THIS fails, no matter whether doc or docx is given in the file name, with COM error "invalid object" _Word_DocSaveAs( $oWord, "my_absolute_path\autoIt\Test.doc" ) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Could not save document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf ; open or create document ; THIS fails in word.au if the document was created and succeeds if an existing document was opened Global $oRange = _Word_DocRangeSet($oDoc, -2) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "RangeError." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
water Posted January 9, 2014 Posted January 9, 2014 Welcome to AutoIt and the forum! You won't find too many examples for Word on the forum. The UDF was completely rewritten for AutoIt 3.3.10.x and is incompatible to the 3.3.8.1 version. The code you posted is only a part of the whole script? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Vaerming Posted January 9, 2014 Author Posted January 9, 2014 Here is the entire script. I basically modified samples from the documentation. The script works if the document exists, regardless of its format, i.e. both "doc" and "docx" formats work. The script fails if a new document is created: Neither could the new document be saved nor could a range be retrieved. expandcollapse popup#include <Word.au3> #include <MsgBoxConstants.au3> #include <File.au3> Global $oWord = _Word_Create() If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $files = _FileListToArray( "C:\Users\XXXXXX\Documents\autoIt", Default, 1 ) Local $bFound = False Local $fileName = "Test.doc" For $i = 1 To $files[0] Step 1 If $files[$i] = $fileName Then $bfound = True ExitLoop EndIf Next Global $oDoc If $bFound = True Then $oDoc = _Word_DocOpen($oWord, "C:\Users\XXXXXX\Documents\autoIt\Test.doc", Default, Default, True) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", "C:\Users\XXXXXX\Documents\autoIt\Test.doc" & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Else _Word_DocAdd( $oWord ) $oDoc = _Word_DocGet( $oWord ) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Error accessing collection of documents." & @CRLF & "@error = " & @error & ", @extended = " & @extended) If IsObj($oDoc) Then MsgBox($MB_SYSTEMMODAL, "", "The variable is an object") Else MsgBox($MB_SYSTEMMODAL, "", "The variable is not an object") EndIf _Word_DocSaveAs( $oWord, "C:\Users\XXXXXX\Documents\autoIt\Test.doc" ) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Could not save document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Global $oRange = _Word_DocRangeSet($oDoc, -2) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "RangeError." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocPictureAdd($oDoc, "C:\Users\XXXXXX\Documents\autoIt\main.png", Default, Default, $oRange) ;_Word_DocPictureAdd($oDoc, "C:\Users\XXXXXX\Documents\autoIt\main.png", Default, Default) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Error adding the picture to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Picture has successfully been added at the end of the document.")
Solution water Posted January 9, 2014 Solution Posted January 9, 2014 Try: expandcollapse popup#include <Word.au3> #include <MsgBoxConstants.au3> #include <File.au3> Global $oWord = _Word_Create() If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $files = _FileListToArray( "C:\Users\XXXXXX\Documents\autoIt", Default, 1 ) Local $bFound = False Local $fileName = "Test.doc" For $i = 1 To $files[0] Step 1 If $files[$i] = $fileName Then $bfound = True ExitLoop EndIf Next Global $oDoc If $bFound = True Then $oDoc = _Word_DocOpen($oWord, "C:\Users\XXXXXX\Documents\autoIt\Test.doc", Default, Default, True) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", "C:\Users\XXXXXX\Documents\autoIt\Test.doc" & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Else $oDoc = _Word_DocAdd($oWord) ; <== Changed If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Error accessing collection of documents." & @CRLF & "@error = " & @error & ", @extended = " & @extended) If IsObj($oDoc) Then MsgBox($MB_SYSTEMMODAL, "", "The variable is an object") Else MsgBox($MB_SYSTEMMODAL, "", "The variable is not an object") EndIf _Word_DocSaveAs( $oDoc, "C:\Users\XXXXXX\Documents\autoIt\Test.doc" ) ; <== Changed If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "Could not save document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Global $oRange = _Word_DocRangeSet($oDoc, -2) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _ "RangeError." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocPictureAdd($oDoc, "C:\Users\XXXXXX\Documents\autoIt\main.png", Default, Default, $oRange) ;_Word_DocPictureAdd($oDoc, "C:\Users\XXXXXX\Documents\autoIt\main.png", Default, Default) If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Error adding the picture to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPictureAdd Example", _ "Picture has successfully been added at the end of the document.") Changed lines are marked with "; <== Changed" My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
water Posted January 9, 2014 Posted January 9, 2014 BTW: Couldn't this code Local $files = _FileListToArray( "C:\Users\XXXXXX\Documents\autoIt", Default, 1 ) Local $bFound = False Local $fileName = "Test.doc" For $i = 1 To $files[0] Step 1 If $files[$i] = $fileName Then $bfound = True ExitLoop EndIf Next be replaced with function FileExists? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Vaerming Posted January 9, 2014 Author Posted January 9, 2014 Thanks for pointing out my bugs, shame on me! And just in case somebody else stumbles upon this: saving as docx seems to expect 16 as format specifier (probably 2007 and later). As for FileExists, I simply could not find it so made it up myself.
water Posted January 9, 2014 Posted January 9, 2014 Correct. If you open a word document of type DOC and want to save it as DOCX you need specify parameter $iFileFormat = $WdFormatDocumentDefault. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
DapperGiant Posted July 17, 2015 Posted July 17, 2015 And just in case somebody else stumbles upon this: saving as docx seems to expect 16 as format specifier (probably 2007 and later).Thank you! This saved me much heartache!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now