【发布时间】:2014-04-19 04:34:53
【问题描述】:
我有一个从 Excel 运行的 VBA 宏,用于从 Word 文档中导入一些信息。在开始解析之前,我想摆脱目录和其他字段。下面的代码(精简到最低限度)在 Office 2010 中有效,但在 Office 2013 中无效。我收到 “对象字段中的方法删除失败” 错误消息,但我不明白为什么。
感谢您的任何提示!
博
Sub ImportBOD()
Dim wdFileName As Variant
Dim WordApp As Object
Dim wdDoc As Word.Document
Dim fld As Word.Field
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", ,"Choose the Word document")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set WordApp = CreateObject("Word.Application")
Set wdDoc = WordApp.Documents.Open(wdFileName, ReadOnly:=True)
'Get rid of table of contents and other fields
For Each fld In wdDoc.ActiveWindow.Document.Fields
fld.Delete
Next
wdDoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
【问题讨论】:
-
删除时,您应该从集合中的最后一项转到第一项。请参阅下面的答案+我的评论。