aiter

word com error trapping possible bug in udf

15 posts in this topic

#1 ·  Posted (edited)

I am testing intercepting com errors in Word and I have found a possible bug in the UDF, it might not be a bug it just means a workaround.

I have found the com error bug in _Word_Create

Func _Word_Create($bVisible = Default, $bForceNew = Default)
    Local $oAppl, $bApplCloseOnQuit = False
    If $bVisible = Default Then $bVisible = True
    If $bForceNew = Default Then $bForceNew = False
    If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application")  <------- here is the line which causees com error

This happens in the following example

#include <Word.au3>
main()
func main()
local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True)
$myrange =  $oDoc.Range
$myrange.Select
sleep(3000)
_Word_DocClose($oDoc)
_Word_Quit($oWord)
endfunc


; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

This script causes a com error to occur and it has the following error

test.au3 (53) : ==> COM Error intercepted !
    err.number is:      0x80020006
    err.windescription: Unknown name.

    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  53
    err.retcode is:     0x00000000

Line 53 is

If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application")

as I mentioned above.

When I force a new instance of Word, no error occurs eg

#include <Word.au3>
main()
func main()
local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
$oWord = _Word_Create(True,True)
$oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True)
$myrange =  $oDoc.Range
$myrange.Select
sleep(3000)
_Word_DocClose($oDoc)
_Word_Quit($oWord)
endfunc

So it seems that if I want to open up  existing Word instance (use _Word_Create() ) I will have to create my own _Word_Open to bypass this problem.

(If Word is already open the problem will not occur)

Comment?

 

Edited by aiter
Correction

Share this post


Link to post
Share on other sites



Which version of AutoIt do you run?


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

the latest version

Share this post


Link to post
Share on other sites

"Unknown name" is no error. It's an information that is handled by the UDF.
Means: The UDF checks if there is a running Word instance. If true it connects to this instance. If false, it creates a new one.

As - starting with AutoIt > 3.3.12.0 - all COM errors are now handled (means: ignored) in the _Word* functions you can't grab COM errors without a lot of modifications in this functions.


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

#5 ·  Posted (edited)

Running 3.3.14.2

Are you saying my com error handling is redundant?

When I put in my own com error handling as posted above, I encounter the com error (i.e it hits my com error function).

If I don't have the the com error handling and run it from dos within a framework, the program just hangs for doomsday, so I need the error handling. The reason it hangs is because any input that is required (whether console input or windows input) hangs it within the framework.

Anyway, I only need a new instance of word so no issue for me, but anyone else will encounter it if they have the same sort of script.

Edited by aiter

Share this post


Link to post
Share on other sites

When Jon changed the way COM errors are handled he needed to insert a COM error handler into the Excel and Word UDF.
It was implemented by inserting a local COM error handler into (nearly) each function. _Word_Create misses this error handler so you can grab the COM error with your global COM error handler. But this does not work for those functions with a local error handler.

But there should b no need to implement a global COM error handler. Check @error after each function call - that's all it needs to grab errors.


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

#7 ·  Posted (edited)

Unfortunately without my global com error handler the program hangs within the framework I run it in as I tried to explain the post above.  I see that the normal com error handler works nicely with a dialog if I run it from dos. When I run it within my framework (database system) it hangs because an error dialog is an input (okay, not this particular problem, but another one, say $oRange.lotofjunk. By using my own com error handler I can intercept this and output the message to console) . I will post the link to another problem I had just now.

Edited by aiter

Share this post


Link to post
Share on other sites

The Word UDF should never create a GUI to output error information. If it does then it's a bug.
Ignore the "unknown name" message by _Word_Create. I would be interested in all other messages originating by the Word UDF.
 


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

It does

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Word.au3>
main()
func main()
;local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True)
$myrange =  $oDoc.Rangeer  ;<--- deliberate error
$myrange.Select
sleep(3000)
_Word_DocClose($oDoc)
_Word_Quit($oWord)
endfunc

Run from cmd prompt, I get

 

 

error.png

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

It comes down to wanting apps compiled for CUI (console) to have pure console error output messages. No inputs (dialog or otherwise) required.

Edited by aiter

Share this post


Link to post
Share on other sites
8 minutes ago, aiter said:

It comes down to wanting apps compiled for CUI (console) to have pure console error output messages.

I think it comes down to write error free code or to handle errors correctly.
How do you expect the second line to work when line 1 is not able to create the object?

17 minutes ago, aiter said:

$myrange =  $oDoc.Rangeer  ;<--- deliberate error
$myrange.Select

I suggest

$oRange =  $oDoc.Rangeer  ;<--- deliberate error
If @error Then 
    ; Write error to log ...
    Exit
EndIf
$oRange.Select

 


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

If you still need to suppress error messages in a GUI you could use trancexx' solution I asked for many years ago:

 


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

#14 ·  Posted (edited)

Water, you biscuit :)

I thought I was alone in this problem.

I agree with you about error free code. The example was just a quick and nasty (when compiled though, it generates the error dialog before it hits the @error clause anyway)

The actual app I am writing I do error check everything, but I wanted what you posted above  just in case the unexpected happened.

Definitely "I stand on the shoulders of giants"

Edited by aiter

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

:D
If you like the solution then there is a button to tell me ;)

Edited by water
1 person likes this

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

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

  • Similar Content

    • Neonovaz
      By Neonovaz
      Hello
       
      Is there anyway to store word documents in Autoit GUI? For example I have a instruction sheet that I want to bundle up with the exe.

      So a user simply clicks the icon and the stored document will launch  (Something like how you can add objects like excel sheets in word documents )

      (I Know we can launch word files from script directory)

       
    • Jury
      By Jury
      I've failed to find an example of _Word_DocFindReplace which searches for formatted text (I'm looking for stand alone paragraph marks that are formatted other than normal i.e. Bold Italic, Underlined). 
      The reason being that when converting a Word document to html one of the main problems in the results is that a stand alone paragraph mark is converted to an html space that retains the formatting ...>&nbsp;<... thus showing up as a underline _  in a browser when it should be blank.  I've played around with the script and got it to at least un-bold  the first paragraph mark regardless if it was bold or not but I'd like to clear all formatting from any stand alone paragraph marks in the whole document.  Below is what I've done so far (not much more than in the help file I'm afraid) .  Way down at the bottom of the _Word_DocFindReplace  help  text is this parameter but without any examples to be found :
      $bFormat   [optional] True to have the find operation locate formatting in addition to or instead of the find text (default = False) #include <MsgBoxConstants.au3> #include <Word.au3> $processing = @MyDocumentsDir & '\AutoIt_code\getter\processing\' Global $oWord = _Word_Create() Global $sTestfile = $processing & "Testing.docx" ConsoleWrite($sTestfile & @CRLF) Global $oDoc = _Word_DocOpen($oWord, $sTestfile) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "ERROR", "Error opening file = '" & $sTestfile & "'" & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound = _Word_DocFind($oDoc, "^p", Default, Default) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error locating paragraph control character in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound.Bold = False If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error inserting text after the paragraph control character in the document." & @CRLF & "@error = " & @error & _ ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", "Paragraph control character successfully replaced." & @CRLF & _ "Text inserted in paragraph 2.")  
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning everyone
      I am working on a little script, which takes some data from a SQLite DB and should create a sort of report, inserting rows in a Word Document... I arrived at the point of:
      _Word_DocTableWrite() and, I don't know how to set the range parameter? What does that specify? 
      Thanks a lot for the help
      EDIT:
      Managed to write a table in the Word document, but now I get an error when I save the document with _Word_DocSaveAs(), with error 2.
      Which are possible causes? Thanks a lot, again
      EDIT 2:
      ... And, how can I set a border to the table? Maybe, with a sort of auto-formatting for text ( larger is the text, larger is the height/width of the table's cell ).
      Thanks  
      EDIT 3 ( bug ):
      Including the parameter $WdSaveChanges in the function _Word_DocSaveAs(), a save dialog box appears, and it should not do it, as it's written in the MSDN documentation:
      wdSaveChanges -1 Save pending changes automatically without prompting the user. Thanks again for everyone will answer to me  
    • anthonyjr2
      By anthonyjr2
      I'm using the Word UDF for the first time, and I'm having some trouble with _Word_DocFind(). There isn't really much talk around the forums about this so it's hard to find any support on the issue I'm having. Here's my code:
      #include <Word.au3> $listPath = @ScriptDir & "\AMCH OFFSET 042617.docx" $pWord = _Word_Create() $oWord = _Word_DocOpen($pWord, $listPath) Local $ctr = 0 Local $searchRange = _Word_DocFind($oWord, "Claim Number") If Not @error Then $ctr += 1 EndIf While ($searchRange <> 0) $searchRange = _Word_DocFind($oWord, "Claim Number", 0, $searchRange) If Not @error Then $ctr += 1 EndIf $searchRange.Select WEnd My problem is that it doesn't seem to find a match of the string on any page after the second. When I run the script, it just loops indefinitely on the second page. I can't post an example of the word document because it is medical data, but every page is basically the same and every page has the string I am looking for on it. Also I tried checking @error after doing a find and it is never set, so I don't think that's the problem.
    • Alin86
      By Alin86
      Hello, in my own design of small procedures, used to insert the ellipse notation in the GUI graphics, and let the arrow pointing to the little girl mouth position. I discovered that point to the gap region size is not fixed, first thought he had just set the angle, calculate the arc starting coordinates and end coordinates OK, then I found that I was wrong. The online search information, for a long time did not find the answer, only to the official website to help you, because I do not start.

      #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <Array.au3> Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client $nPI = 3.1415926535897932384626433832795 $iAngle = 95 $iSpace = 50 $nX = 100 $nY = 200 $nWidth = 200 $nHeight = 100 $fStartAngle = $iAngle + $iSpace / 2 $fSweepAngle = 360 - $iSpace _GDIPlus_Startup() Local Const $iWidth = 600, $iHeight = 600 Local $hGUI = GUICreate("GDI+ UDF 示例", $iWidth, $iHeight) GUISetState(@SW_SHOW) Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) Local $hPen = _GDIPlus_PenCreate(0xFFFF8080, 1) Local $hImage = _GDIPlus_ImageLoadFromFile("g_8.png") _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 400, 100, 102, 278) _GDIPlus_GraphicsDrawArc($hGraphics, $nX, $nY, $nWidth, $nHeight, $fStartAngle, $fSweepAngle, $hPen) ; Local $a = $nWidth / 2 Local $b = $nHeight / 2 Local $c = Sqrt($a ^ 2 - $b ^ 2) Local $Coordinate[4] $Coordinate[0] = Sqrt($a ^ 2 * $b ^ 2 / ($a ^ 2 * Tan((360 - $iAngle - $iSpace / 2) * $nPI / 180) ^ 2 + $b ^ 2)) $Coordinate[1] = Tan((360 - $iAngle - $iSpace / 2) * $nPI / 180) * $Coordinate[0] ;_ArrayDisplay($Coordinate) If (360 - $iAngle - $iSpace / 2) >= 90 And (360 - $iAngle - $iSpace / 2) <= 270 Then $Coordinate[0] = -$Coordinate[0] If (360 - $iAngle - $iSpace / 2) >= 0 And (360 - $iAngle - $iSpace / 2) < 180 Then $Coordinate[1] = -$Coordinate[1] $Coordinate[0] = $nX + $a + $Coordinate[0] $Coordinate[1] = $nY + $b + $Coordinate[1] ;MouseMove($Coordinate[0], $Coordinate[1]) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE _GDIPlus_PenDispose($hPen) _GDIPlus_ImageDispose($hImage) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_Shutdown() GUIDelete($hGUI)