Microsoft Word宏复制文本并在查找和替换中将其过去

时间:2016-06-28 07:08:13

标签: vba ms-word word-vba

我想将复制的文字粘贴到.Replacement.Text中。我用Selection Paste尝试了这个,但这不起作用。

要明确:我正在尝试复制Microsoft Word文档中的文本,并且我希望复制的文本更改为多个字段代码 ALT + F9 。所以我的Word文档会立刻更改所有字段代码。

    Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    Selection.Copy
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "00000"
        .Replacement.Text ="Selection.Paste"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.WholeStory
    Selection.Fields.Update
End Sub

1 个答案:

答案 0 :(得分:0)

无需使用Selection.WholeStory

    Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
        Selection.Copy
        ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
'        Selection.WholeStory
    searchText=selection.text
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "00000"
            .Replacement.Text =searchText
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
        Selection.WholeStory
        Selection.Fields.Update
    End Sub