Kay
Kay

Reputation: 2854

Optimizing Find and Replace MS-Word VBA Script

I wrote a macro for replacing the format of the first of three spaces followed by a certain string with a digit in blue font and another one for replacing the space inbetween brackets followed by a certain string.

Do you know how to optimize these two procedures (I use wildcards of the search and replace dialogue of the MS-Word but guess it is rather awkard to use this in VBA..)?

My macros:

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

Upvotes: 0

Views: 3712

Answers (1)

Mike
Mike

Reputation: 312

I'm not entirely sure what you're trying to do because your code does work. When you say optimize, are you asking if there's a quicker way to do it, or are you asking if your code can be shortened? I can't see any reason for the dimensions you set at the beginning, so if you're just looking for shorter code, you could use the following:

    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

Based on the fact that you have those dimensions, and you used a number for one of them, I can't help but think that you were actually trying to create a numbering system, where each instance is given a number. If that's the case, here's the code you would use:

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

Upvotes: 0

Related Questions