Word 2003 vs. 2007/2010 Automation
I'm automating Word in a VB6 application and I'm getting an exception when the host machine is using Word 2007 or 2010 but not on a machine running Word 2003. The exception is
Runtime error '4605': "This method or property is not available because the object refers >to the end of a table row."
The exception is thrown on the seventh iteration of the following loop, on the line which attempts to insert a page break:
For num = 1 To breakArrCount
Set myRange = oworddoc.Paragraphs(breakArr(num)).Range
With myRange
On Error Resume Next
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
Next
The array, breakArr, stores the indexes of the paragraphs in the document which are to be followed by a page break.
Interestingly enough, if I break out of the loop prior to the exception开发者_如何学运维 (i.e. after the 6th iteration) and view the generated document, I notice that the page breaks (for the first 6 pages) are inserted in different points in Word 2007/2010 than in 2003.
Why would there be a difference? Does anybody know what I should be doing differently?
Thanks.
Welcome to the world of developing Multiversion targeted Word automation apps.
Seriously though, you're going to hit +mountains+ of these types of issues. I know that different versions of word handle inserting page breaks at different places in different ways. I had tons of problems with then developing an addin that intelligently merged multiple word documents into a single, normal (not master) document.
Best thing is to test like heck and build exception routines to handle specific versions of Word when you get anomalies like this. Hopefully, you won't run into many, but I can say there's a LOT of em out there.
In this particular case, the paragraph selected is likely in a table, and you're collapsing to the END of the para, then trying to insert something, which is messing with the End of cell marker for the cell you're in. Different versions of word handle that differently. In one case (I forget specifically which), if you do that in the VERY LAST CELL in the table, you'll corrupt the document when you save it, but you won't know that till you try to reopen the document.
Often, you can work around this specific prob by detecting you're in a table, and if so, collapse to the end, then back up one char (if possible, the cell might be blank) and THEN inserting your page break.
精彩评论