仅在选择中查找和替换

Find and Replace in just the selection

我有以下宏。它将 x.x 形式的数字更改为 x,x。它被记录下来,我添加了 IF 语句以确保选择文本以防止用户在整个文档上这样做。

 Sub fixComma()
    '
    ' fixComma Macro
    '
    '
      If (Selection.Start <> Selection.End) Then
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.LanguageID = wdEnglishUS
        With Selection.Find
            .Text = "([0-9]).([0-9])"
            .Replacement.Text = ","
            .Forward = True
            .Wrap = wdFindAsk
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchByte = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With

        Selection.Find.Execute Replace:=wdReplaceAll
         Else
           MsgBox "Nothing is selected, Macro terminated"
        End If
    End Sub

问题是它正在更改整个文档,而不仅仅是选择。

改变

 Selection.Find.Execute Replace:=wdReplaceAll

 Selection.Find.Execute Replace:=wdReplaceOne

将得到它,因此选择中 x.x 的第一个实例将更改为 x,x 而不是整个文档。

编辑: 如果您只想更改选定区域中的所有项目,则保留:

Selection.Find.Execute Replace:=wdReplaceAll

但改变

.Wrap = wdFindAsk

.Wrap = wdFindStop