MS WORD - 删除字段代码,保留标题中的值

时间:2016-06-12 11:31:08

标签: vba

我有这个Word VBA代码,它删除了域代码,但保留了它们的值。这很好用,但不在标题中。如何编辑它以适用于文档正文(以及页眉/页脚)?

Sub RemoveFieldCodeButRetainValue()
    Dim d As Document
    Dim iTemp As Integer
    Dim strTemp As String

    Set d = ActiveDocument

    For iTemp = d.Fields.Count To 1 Step -1

        strTemp = d.Fields(iTemp).Result

        d.Fields(iTemp).Select

        With Selection
            .Fields(1).Delete
            .TypeText strTemp
        End With
    Next
End Sub

2 个答案:

答案 0 :(得分:0)

好吧,我明白了:

使用两个宏:

Sub CtrlAPlusFNine()
    Selection.WholeStory
    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
lbl_Exit:
    Set oStory = Nothing
    Exit Sub
End Sub


Sub RemoveFieldCodeButRetainValue()
    Dim d As Document
    Dim iTemp As Integer
    Dim strTemp As String
    Set d = ActiveDocument
    For iTemp = d.Fields.Count To 1 Step -1
        strTemp = d.Fields(iTemp).Result
        d.Fields(iTemp).Select
        With Selection
            .Fields(1).Delete
            .TypeText strTemp
        End With
    Next
End Sub

..并使用Application.Run

从第三个宏调用这两个

答案 1 :(得分:0)

对不起,我意识到这并不是问题的答案,而是使用:

For Each fld In ActiveDocument.Fields
    fld.Unlink
Next

将在删除基础字段时保留该值。据我所知,您可以使用相同的技术在遍历其他故事中针对页眉/页脚区域的建议的各种故事范围中。