none
MS Word VBA or C# - issue with searching a specific text not preceded by comma RRS feed

  • Question

  • My following macro is supposed to replace "including" with "TEST" provided "including" is not preceded by comma. Macro is doing find except that it removes the space before "including". How can I get the desired output shown below? Thanks..Nam

    Input: This is a test including and another test ,including with some including test.

    Output: This is a testTEST and another test ,including with someTEST test.

    Desired Output: This is a test TEST and another test ,including with some TEST test.

    Sub testMacro()
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "[!,]including"
            .Replacement.Text = "TEST"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        
    End Sub

    Sunday, January 24, 2016 7:00 PM

Answers

  • You could use:

    Sub testMacro()
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "([!,])including"
            .Replacement.Text = "\1TEST"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll   
    End Sub

    That said, punctuation like "text ,including" is wrong - it should be "text, including".


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Monday, January 25, 2016 2:02 AM
    Monday, January 25, 2016 12:05 AM

All replies

  • You could use:

    Sub testMacro()
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "([!,])including"
            .Replacement.Text = "\1TEST"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll   
    End Sub

    That said, punctuation like "text ,including" is wrong - it should be "text, including".


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Monday, January 25, 2016 2:02 AM
    Monday, January 25, 2016 12:05 AM
  • Paul, thank you, it works. Thank you also for correcting the punctuation. -Nam
    Monday, January 25, 2016 2:01 AM