删除Word字段VBA不能在Word 2013中工作,在Word 2010中工作

我有一个从Excel运行的VBAmacros从Word文档导入一些信息。 在我开始parsing之前,我想摆脱目录和其他领域。 下面的代码(剥离到最低限度)在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 

必须尝试通过索引删除它们吗?

 For i = 1 to wdDoc.ActiveWindow.Document.Fields.Count wdDoc.ActiveWindow.Document.Fields.Item(i).Delete Next i 

我发现有时在循环中删除活动对象,在这种情况下, fld并不总是成功,VBA错误消息不是特定的。 此外,似乎SEQ(序列字段)和XE(IndexEntry字段)不能被Unlink ,这暗示了Delete可能会失败,尽pipe微软并没有指定这种情况。

编辑

基于评论循环上次到第一

 For i = wdDoc.ActiveWindow.Document.Fields.Count To 1 Step -1 wdDoc.ActiveWindow.Document.Fields.Item(i).Delete Next i