Jump to content

Word.au3 Error


Herb191
 Share

Recommended Posts

The following works on one of my computers running XP but when I run it on a computer with Windows 7 I get errors. Any ideas on how I can fix it? The example script in AutoIt’s help file also does not work on the computer with Windows 7. I checked the include file and the word.au3 file is there.

#include <Word.au3>

$oWordApp = _WordCreate("", 0, 0)
_WordQuit($oWordApp, 0)

--> Word.au3 Error from function _WordDocAdd, $_WordStatus_InvalidObjectType

--> Word.au3 Error from function _WordQuit, $_WordStatus_InvalidObjectType

Link to comment
Share on other sites

You must use "your own error handler function" which contains Messagebox() with all the errors. See examples.

Perhaps I am not understanding something correctly. I tried running this and got the same thing.

#include <Word.au3>
Global $oWordErrorHandler

_WordErrorHandlerRegister("MyErrFunc")
$oWordErrorHandler = _WordCreate("", 0, 0)
_WordErrorHandlerDeRegister()
_WordQuit($oWordErrorHandler, 0)

Exit

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oWordErrorHandler
    $ErrorScriptline = $oWordErrorHandler.scriptline
    $ErrorNumber = $oWordErrorHandler.number
    $ErrorNumberHex = Hex($oWordErrorHandler.number, 8)
    $ErrorDescription = StringStripWS($oWordErrorHandler.description, 2)
    $ErrorWinDescription = StringStripWS($oWordErrorHandler.WinDescription, 2)
    $ErrorSource = $oWordErrorHandler.Source
    $ErrorHelpFile = $oWordErrorHandler.HelpFile
    $ErrorHelpContext = $oWordErrorHandler.HelpContext
    $ErrorLastDllError = $oWordErrorHandler.LastDllError
    $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0, "COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc   ;==>MyErrFunc
Edited by Herb191
Link to comment
Share on other sites

#include <Word.au3>

_WordErrorHandlerRegister("MyErrFunc")
$oWord = _WordCreate("", 0, 0)
_WordQuit($oWord, 0)
_WordErrorHandlerDeRegister()
Exit

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oWordErrorHandler
    $ErrorScriptline = $oWordErrorHandler.scriptline
    $ErrorNumber = $oWordErrorHandler.number
    $ErrorNumberHex = Hex($oWordErrorHandler.number, 8)
    $ErrorDescription = StringStripWS($oWordErrorHandler.description, 2)
    $ErrorWinDescription = StringStripWS($oWordErrorHandler.WinDescription, 2)
    $ErrorSource = $oWordErrorHandler.Source
    $ErrorHelpFile = $oWordErrorHandler.HelpFile
    $ErrorHelpContext = $oWordErrorHandler.HelpContext
    $ErrorLastDllError = $oWordErrorHandler.LastDllError
    $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0, "COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc   ;==>MyErrFunc

Link to comment
Share on other sites

#include <Word.au3>

_WordErrorHandlerRegister("MyErrFunc")
$oWord = _WordCreate("", 0, 0)
_WordQuit($oWord, 0)
_WordErrorHandlerDeRegister()
Exit

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oWordErrorHandler
    $ErrorScriptline = $oWordErrorHandler.scriptline
    $ErrorNumber = $oWordErrorHandler.number
    $ErrorNumberHex = Hex($oWordErrorHandler.number, 8)
    $ErrorDescription = StringStripWS($oWordErrorHandler.description, 2)
    $ErrorWinDescription = StringStripWS($oWordErrorHandler.WinDescription, 2)
    $ErrorSource = $oWordErrorHandler.Source
    $ErrorHelpFile = $oWordErrorHandler.HelpFile
    $ErrorHelpContext = $oWordErrorHandler.HelpContext
    $ErrorLastDllError = $oWordErrorHandler.LastDllError
    $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0, "COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc   ;==>MyErrFunc

Hmmm when I run that I still get the same thing:

--> Word.au3 Error from function _WordDocAdd, $_WordStatus_InvalidObjectType

--> Word.au3 Error from function _WordQuit, $_WordStatus_InvalidObjectType

+>20:29:22 AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 1.781

Link to comment
Share on other sites

  • Developers

There is no COM error but a "check in the UDF error".

Looks like ObjName() on line 1803 in word.au3 returns "" and @error=1 making the Word UDFs fail.

This needs investigation...

Func __WordIsObjType(ByRef $o_object, $s_type)
    If Not IsObj($o_object) Then Return SetError($_WordStatus_InvalidDataType, 1, 0)

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "internal function __WordIsObjType", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)
    ;
    Local $s_Name = ObjName($o_object), $objectOK = False  ; ObjName() returning ""
    Switch $s_type
        Case "wordobj"
            If __WordIsObjType($o_object, "application") Then
                $objectOK = True
            ElseIf __WordIsObjType($o_object, "window") Then
                $objectOK = True
            ElseIf __WordIsObjType($o_object, "document") Then
                $objectOK = True
            EndIf
        Case "application"
            If $s_Name = "_Application" Then $objectOK = True
        Case "document"
            If $s_Name = "_Document" Then $objectOK = True
        Case "documents"
            If $s_Name = "Documents" Then $objectOK = True
        Case "range"
            If $s_Name = "Range" Then $objectOK = True
        Case "window"
            If $s_Name = "Window" Then $objectOK = True
        Case "windows"
            If $s_Name = "Windows" Then $objectOK = True
        Case Else
            ; Unsupported ObjType specified
            Return SetError($_WordStatus_InvalidValue, 2, 0)
    EndSwitch
 $objectOK = True
    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    If $objectOK Then
        Return SetError($_WordStatus_Success, 0, 1)
    Else
        Return SetError($_WordStatus_InvalidObjectType, 1, 0)
    EndIf

EndFunc   ;==>__WordIsObjType

I get the same as the OP on Win7 & Word2007.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So is there any kind of workaround?

Sure, disable the test temporary in Word.au3 or be patient till somebody has the time to sort out why the ObjName() function returns "" and @error = 1 and see what a proper fix is for the issue.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 11 months later...

The last post worked for me. I tryed to create a word object with this code:

$wordApp = ObjCreate("Word.Application")

$odoc = $wordApp.Documents.Open("") ;set the file to open

$wordApp.visible = true

sleep(2000)

$wordApp.Quit

instead of usind the regular way and it worked. Not sure exactly where the mistake is but there is something to do with te COM objects of Microsoft word.

#include <Word.au3>

$oWord = _WordCreate("", 0, 0)

_WordQuit($oWord, 0)

I have two PCs with windows 7 and office 2010. One of them works just fine

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