我有一个从Excel运行的VBA宏,用于从Word文档导入一些信息。在我开始解析之前,我想摆脱目录和其他字段。下面的代码(剥离到最低限度)适用于Office 2010,但不适用于Office 2013.我收到“对象字段中的方法删除失败” eror消息,但我不明白为什么。删除Word字段VBA不能在Word 2013中工作,在Word 2010中工作
感谢您的任何提示!
博
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
。看到下面的回答+我的评论。 –