RichardL Posted January 31 Posted January 31 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 expandcollapse popupOpt('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
argumentum Posted January 31 Posted January 31 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.
RichardL Posted January 31 Author Posted January 31 @argumentum thanks but I tried Office 2007, and even after 4 months my fingers didn't know where anything is. So I went back to 2003. argumentum 1
Nine Posted January 31 Posted January 31 I tested your code on Office 2003 (yes, yes, I still have it ). 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...) argumentum 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
RichardL Posted January 31 Author Posted January 31 @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.
Nine Posted January 31 Posted January 31 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
RichardL Posted February 6 Author Posted February 6 I repeated the program in vbs, and running it on the same computer it's able to run for 5000 loops where the AutoIt breaks after 249. Set oWord = CreateObject("Word.Application") oWord.Visible = True oWord.Documents.Open "C:\AutoIt_Word_Test\Test.doc" WScript.Echo oWord.Documents(1).Name & " " & oWord.Documents(1).Paragraphs.count Set oWDoc = oWord.Documents(1) WScript.Echo oWDoc.Words.Count iLoopCount = 0 For iLoopCount = 1 To 5000 For iParaIx = 1 To 10 iColrIx = oWDoc.Paragraphs(iParaIx).Range.Font.ColorIndex If iColrIx <> 1 Then WScript.Echo "Loop " & iLoopCount & ", P " & iParaIx & " " & oWDoc.Paragraphs(iParaIx).Range.Text sParaText = 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 = oWDoc.Paragraphs(iParaIx).range.Words(iWrdIx).Text Next End If Next Next WScript.Echo "End " & oWDoc.Paragraphs(iParaIx).Range.Text Run: cscript Word_Read_Test.vbs I tried the same programs on another PC. W10 Enterprise 22H2. AutoIt 3.3.14.5, Microsoft Office Word 2003 (11.5604,5606). The AutoIt program runs fine, >6000 loops without problem. The vbs is also fine, and clearly about twice as fast as the AutoIt. I'm not sure what to think. I have possible workarounds using AutoIt in 2 passes, or vbs, or the the other PC. I haven't thought about the patch situation yet. The first PC has SP3, the other doesn't.
Nine Posted February 6 Posted February 6 Old PC, old OS, old AutoIt, old Office. Maybe the Bits&Bytes God is trying to tell you something SOLVE-SMART 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
RichardL Posted February 6 Author Posted February 6 Hadn't realised there was a newer AutoIt - I'll try that.
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