2

以下代码有效,但它对整个文档执行所有操作。我想突出显示一段文本,然后当我运行宏时,它只适用于突出显示的文本。我怎么做?谢谢...

Sub DoCodeNumberStyle(numchars As String)

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(^13)([0-9]{" + numchars + "}) "
        .Replacement.Text = "\1###\2$$$ "
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("CodeNumber")
    With Selection.Find
        .Text = "###([0-9]{" + numchars + "})$$$"
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub CodeNumberStyle()
    DoCodeNumberStyle ("1")
    DoCodeNumberStyle ("2")
End Sub

后记:

我发现的另外一件事:如果您在一个选择上做了多个查找,第一个查找会丢失/更改选择,所以其他的不再受原始选择的限制(并且 wdReplaceAll 将继续到文档)。要解决此问题,请将选择捕获到范围中。这是我的方法的最终版本,它现在可以完成我需要的一切,仅限于原始突出显示的选择(即使有 3 个查找和替换),并且在代码方面也已最小化:

Sub AAACodeNumberStyleHighlightedSelection()

    With Selection.Range.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles("Code")
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False

        ' First line:
        .Text = "1   //"
        .Replacement.Text = "###1$$$   //"
        .MatchWildcards = False
        .Execute Replace:=wdReplaceAll

        ' Rest of lines:
        .Text = "(^13)([0-9]{1,2}) "
        .Replacement.Text = "\1###\2$$$ "
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll

        ' Now style the line numbers:
        .Text = "###([0-9]{1,2})$$$"
        .Replacement.Text = "\1"
        .Replacement.Style = ActiveDocument.Styles("CodeNumber")
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll

    End With

End Sub
4

3 回答 3

2

将 .Wrap 更改为 wdFindStop,这应该适合您。我认为这可能是一个小的 Word 错误;文档说 Wrap 值

设置如果搜索开始于文档开头以外的点并到达文档结尾(如果 Forward 设置为 False,反之亦然)或在指定的选择中找不到搜索文本时发生的情况或范围。

但似乎它迫使 Find 转到文档的末尾,而不是考虑选择。无论如何,如果您只打算在选择上运行它,则不需要 wdFindAsk。

于 2013-01-25T18:03:33.397 回答
0

我也发现,即使在某个范围上开始 FIND 循环,该范围也会由 FIND 重新定义,因此 .execute 上的连续循环超出了原始范围到文档末尾。wdFindStop 仅在文档​​的末尾停止,而不是在原始范围的末尾。

所以,我插入了一个 IF 语句:

do while .find.found ... If .find.parent.InRange(doc.Bookmarks("BODY").Range) = False Then Exit Do ... .execute loop

于 2013-10-18T17:54:17.580 回答
0
Set myRange = Selection.Range
 myRange.Select
    With Selection.Find
        .Text = "Apple"
        .Replacement.Text = "Banana"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        '.MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

   myRange.Select
        With Selection.Find
        .Text = "red"
        .Replacement.Text = "yellow"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        '.MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
于 2016-10-18T00:21:36.400 回答