Sign in to follow this  
Followers 0
Vaerming

new user - issues with really simple word script

8 posts in this topic

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)

Share this post


Link to post
Share on other sites



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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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.

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

Share this post


Link to post
Share on other sites

Try:

#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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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!

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