更新Word文档字段

时间:2015-02-02 10:05:26

标签: vba ms-word

我有一个更新文档中所有字段的过程。 但是我想跳过wdFieldDocVariable,项索引应该是Type。

Public Sub MyApplicationUpdate()
hdWriteInfoLog ("BEGIN MACRO:   MyApplicationUpdate")
Dim oTOC As TableOfContents
Dim oField As Field

' Update Fields in all document StoryRanges
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
    oStory.Fields.Update
    If oStory.StoryType <> wdMainTextStory Then
        While Not (oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            oStory.Fields.Update
        Wend
    End If
Next oStory
Set oStory = Nothing

' Update all TablesOfContents
For Each oTOC In ActiveDocument.TablesOfContents
    oTOC.Update
Next oTOC

hdWriteInfoLog ("END MACRO:     MyApplicationUpdate")
End Sub

1 个答案:

答案 0 :(得分:0)

我想,我找到了答案,我相信有更有效的方法可以做到这一点,但至少它有效......

&#39;测试程序 Public Sub MyApplicationUpdate()     hdWriteInfoLog(&#34; BEGIN MACRO:MyApplicationUpdate&#34;)     Dim oTOC As TableOfContents     Dim oField As Field     Dim oField2 As Field

' Update Fields in all document StoryRanges
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
    For Each oField2 In oStory.Fields
        If Not oField2.Type = wdFieldDocVariable Then
            oField2.Update
        End If
    Next oField2

    If oStory.StoryType <> wdMainTextStory Then
        While Not (oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            ' oStory.Fields.Update
            For Each oField2 In oStory.Fields
                If Not oField2.Type = wdFieldDocVariable Then
                    oField2.Update
                End If
            Next oField2
        Wend
    End If
Next oStory
Set oStory = Nothing

' Update all TablesOfContents
For Each oTOC In ActiveDocument.TablesOfContents
    oTOC.Update
Next oTOC

hdWriteInfoLog ("END MACRO:     MyApplicationUpdate")

End Sub