【问题标题】:Optimizing Find and Replace MS-Word VBA Script优化查找和替换 MS-Word VBA 脚本
【发布时间】:2012-11-13 19:26:30
【问题描述】:

我编写了一个宏,用于替换三个空格中的第一个的格式,后跟某个字符串,用蓝色字体的数字替换另一个宏,用于替换括号之间的空格,后跟某个字符串。

你知道如何优化这两个过程吗(我使用 MS-Word 的搜索和替换对话框的通配符,但猜想在 VBA 中使用它是相当尴尬的......)?

我的宏:

Sub replace_3spaces()

Dim str_after As String
Dim re_number As Integer

str_after = "normal"
re_number = "1"

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "([^s]{3})" & "(" & str_after & ")"
        .Replacement.Text = "§§§\2"
        .Forward = True
        .Wrap = wdFindContinue
        .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.Font.ColorIndex = wdBlue
    With Selection.Find
        .Text = "§§§"
        .Replacement.Text = re_number & " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

【问题讨论】:

    标签: vba replace ms-word wildcard


    【解决方案1】:

    我不完全确定您要做什么,因为您的代码确实工作。当您说优化时,您是在问是否有更快的方法来做到这一点,还是在问您的代码是否可以缩短?我看不出您在开始时设置的尺寸有任何原因,所以如果您只是在寻找更短的代码,您可以使用以下代码:

        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([^s]{3})(normal)"
            .Replacement.Text = "§§§\2"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
            .Forward = False
            .ClearFormatting
            .Replacement.Font.ColorIndex = wdBlue
            .Format = True
            .Text = "§§§"
            .Replacement.Text = "1 "
            .Execute Replace:=wdReplaceAll
        End With
    

    基于您拥有这些维度的事实,并且您为其中一个使用了一个数字,我不禁认为您实际上是在尝试创建一个编号系统,其中每个实例都有一个编号。如果是这种情况,您可以使用以下代码:

    Dim str_after, oldColor As String
    Dim re_number As Integer
    
    str_after = "normal"
    re_number = "1"
    
        Selection.HomeKey unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([^s]{3})" & "(" & str_after & ")"
            .Replacement.Text = "§§§\2"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
            While Selection.Find.Execute
                oldColor = Selection.Font.Color
                Selection.Font.Color = wdColorBlue
                Selection.TypeText Text:=re_number & " "
                Selection.Font.Color = oldColor
                Selection.TypeText Text:=str_after
                re_number = re_number + 1
            Wend
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 2021-03-13
      相关资源
      最近更新 更多