Word 2003 与 2007/2010 自动化
我在 VB6 应用程序中自动执行 Word,当主机使用 Word 2007 或 2010 但在运行 Word 2003 的计算机上没有时,我遇到异常。异常是
运行时错误“4605”:“此方法或属性不可用,因为该对象引用了表行的末尾。”
在以下循环的第七次迭代中,在尝试插入分页符的行上抛出异常:
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
数组,breakArr,存储文档中后面跟随分页符的段落的索引。
有趣的是,如果我在异常之前(即第 6 次迭代之后)跳出循环并查看生成的文档,我会注意到分页符(前 6 页)被插入到 Word 2007/2010 中的不同位置与 2003 年相比。
为什么会有差异?有人知道我应该采取什么不同的做法吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到开发多版本目标 Word 自动化应用程序的世界。
但说真的,你会遇到很多这类问题。我知道不同版本的单词处理以不同的方式在不同的位置插入分页符。我在开发一个插件时遇到了很多问题,该插件可以智能地将多个 Word 文档合并为一个普通(非主)文档。
最好的办法是进行测试并构建异常例程,以在遇到此类异常时处理特定版本的 Word。希望你不会遇到很多,但我可以说那里有很多。
在这种特殊情况下,所选的段落可能在表格中,并且您折叠到段落的结尾,然后尝试插入一些内容,这会扰乱您所在单元格的单元格结尾标记。 word 的版本对此的处理方式有所不同。在一种情况下(我具体忘记了),如果您在表中的最后一个单元格中执行此操作,则在保存文档时会损坏文档,但直到您尝试重新打开文档时您才会知道这一点。
通常,您可以通过检测您是否在表格中来解决此特定问题,如果是,则折叠到最后,然后备份一个字符(如果可能,单元格可能为空白),然后插入分页符。
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.