记录"查找+全部替换"当宏作为宏运行时,宏只查找第一个实例

时间:2013-10-29 01:03:34

标签: vba replace ms-word find word-vba

我设置了一个用户Find + Replace All宏,以查找和替换特定文本的所有实例,并按计划运行。

然而,当我将该操作记录为宏并运行它时,它只替换了查找文本的第一个实例。我做错了什么?

录制的代码位于下方。

Sub Macro25()
'
' Macro25 Macro
'
'

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Body Text")
    With Selection.Find.ParagraphFormat
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorBlack
            .BackgroundPatternColor = wdColorBlack
        End With
        .Borders.Shadow = False
    End With
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Body Text 2")
    With Selection.Find.Replacement.ParagraphFormat
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorBlack
            .BackgroundPatternColor = wdColorBlack
        End With
        .Borders.Shadow = False
    End With
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

3 个答案:

答案 0 :(得分:2)

宏仅替换运行宏时选择的文本。这就是Selection这个词的含义。

如果您希望查找/替换整个文档,则需要使用Selection替换宏中的ActiveDocument

答案 1 :(得分:0)

感谢您的回答。 所以继续前进......下面是我自己编写的代码。它基于宏录制器生成的代码。 我的代码没有使用相同的选择理念。它使用rng Range对象。

但是我得到了同样的效果:它只找到了第一个实例。

Function ExecReplaceStyle(strSourceStyle As String, strDestinationStyle As String) As Integer
        On Error GoTo ErrorHandler

        Dim rng As Range
        Dim ret As Integer

        ExecReplaceStyle = 0
        Set rng = docActiveDoc.Range

        With rng.Find
            .ClearFormatting
            .Style = ActiveDocument.Styles(strSourceStyle)
            .Replacement.Style = ActiveDocument.Styles(strDestinationStyle)
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Forward = True
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

        rng.Select
        rng.Find.Execute Replace:=wdReplaceAll


        ExecReplaceStyle = ret

        Exit Function

    ErrorHandler:
        ExecReplaceStyle = Err.Number
        ErrDescription = Err.Description
        Resume Next
    End Function

答案 2 :(得分:0)

Selection.Find.Execute Replace:=wdReplaceAll之后的End With应在整个文档中进行搜索和替换。