Jump to content

Recommended Posts

Posted

I’m using AutoIt to extract data from an office doc.  In outline the format of the doc was a coloured section title (not a heading in the Word sense), then a few lines in black.  I keep getting a com error.  Tried various things to eliminate:

-    Add com error handle – no better, after one failure, all other interaction failed.
-    Remove failing data – it just failed on the next data
-    Disconnect the Word object and reconnect every 100 interactions – no better.
-    Close all Word windows and start again – no better.
-    Run from start of doc to half way, then separately for the second half – Clear!

It really looks like an interaction count limit between AutoIt and Word. I’ve reduced the program and the doc to a minimum.  It usually fails on reading the ColorIndex of a word, but on this occasion reading a word-count.

L  250, P    7  6 HIJ KLM
L  251, P    1 12 ABC DEF
L  251, P    7  6 HIJ KLM
"C:\AutoIt_Word_Test\Word_Read_Test.au3" (39) : ==> The requested action with this object has failed.:
For $iWrdIx = 1 To $oWDoc.Paragraphs($iParaIx).Range.Words.Count
For $iWrdIx = 1 To $oWDoc^ ERROR

I’m surprised I’ve never run into this before as have used AutoIt to drive Word and Excel many times in the last 15 years.

Winows 7 Enterprise SP1, MS Office Word 2003, AutoIt 3.3.14.5


 

Opt('MustDeclareVars', 1)

Func ConFmtWr($sCntrl, $a = "", $b = "", $c = "", $d = "", $e = "", $f = "", $g = "", $h = "", $i = "", $j = "", $k = "", $l = "")
    ConsoleWrite(StringFormat($sCntrl, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l))
EndFunc


Func OpenOrGetWord($sDocPath, ByRef $oWord, ByRef $oWDoc)
    $oWDoc = ObjGet($sDocPath)
    ; (Good if doc is already open or not. Surprise)
    $oWord = $oWDoc.Parent
    $oWord.Visible = 1
    ConFmtWr("WC %4d\n", $oWDoc.Words.Count)
    ConFmtWr("PC %4d\n", $oWDoc.Paragraphs.Count)
EndFunc


Local $oWDoc
Local $oWord
Local $sDocName = @ScriptDir & "\Test.doc"
Local $iWrdIx
Local $iParaIx
Local $iColrIx
Local $sWrdText
Local $iLoops

OpenOrGetWord($sDocName, $oWord, $oWDoc)

$iLoops = 0
While 1
    For $iParaIx = 1 To 10
        $iColrIx = $oWDoc.Paragraphs($iParaIx).Range.Font.ColorIndex
        If $iColrIx <> 1 Then
            ConFmtWr("L %4d, P %4d %2d %s\n", $iLoops, $iParaIx, $iColrIx, $oWDoc.Paragraphs($iParaIx).Range.Text)

            For $iWrdIx = 1 To $oWDoc.Paragraphs($iParaIx).Range.Words.Count
                $iColrIx  = $oWDoc.Paragraphs($iParaIx).Range.Words($iWrdIx).Font.ColorIndex
                $sWrdText = StringStripWS($oWDoc.Paragraphs($iParaIx).range.Words($iWrdIx).Text, 3)
            Next
        EndIf
    Next
    $iLoops += 1
WEnd

 

Test.doc

Posted

Make the .doc a .docx and rename it as .zip. Then everything is an .xml ( well, a bunch of .xml ), the point been that now that is not a .doc there is no limitation.
On the other hand, you don't have the niceties of ".Range.Font.ColorIndex" and what not that comes with the com interface.

I may be giving a non-answer but it is possible with "MS Office Word 2007"
I hope you like the idea. Is a way to not rely on Word other than to save as .docx

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I tested your code on Office 2003 (yes, yes, I still have it :P).  And it is working fine after removing your infinite While loop.  Since your doc has 12 paragraphs, your For loop cannot exceed this limit (in your script you have put 10...)

Posted

@NineThank you for trying it.  The doc I was working on is hundreds of lines long.  The problem happens after about 300 lines.  I added the the while loop just to demo the issue by repeatedly using the first 10 lines of a smaller doc.  For me this goes wrong after about 250 lines, as in the output in the first post. Please try with the loop back in, or use cut and paste in the doc  to make 500 lines.

 

Posted

I reinstated the while loop and went over 600 lines without any problem.  Did you install the cumulative patch for 2003 ?  I did because I had many problems with the original version.

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
×
×
  • Create New...