none
VBA or C# to find and replace specific formatting outside the Math equations in MS Word RRS feed

  • Question

  • Following Macro1 correctly replaces two instances of "a" (outside Math equation) with "b" in the following line in my Word document.

    This  is a test with a new line.

    But when I try to replace the italic with bold font in the above line in Word document using Macro2, it correctly makes the two instance of "a" (outside math equation) bold but it also makes the entire math equation bold as well. As we know, by default, the Math equations in Word are italic. But I want Macro2 to avoid applying Find functionality for Math equation (as Macro1 correctly does so). You can try the above scenario by copying the following two macros in a VBA module of your Word document and using the above example with math equation. Please note that all instances of "a" in the above example are italic : 

    Sub Macro1()

    Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "a" .Replacement.Text = "b" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll

    Sub Macro3()
        Selection.Find.ClearFormatting
        Selection.Find.Font.Italic = True
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Font.Bold = True
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    Thanks..Nam

    Monday, December 14, 2015 12:34 AM

Answers

  • There are various ways of handling this; here's one way:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Font.Italic = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .OMaths.Count > 0 Then
          .End = .OMaths(1).Range.End
        Else
          .Font.Bold = True
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Monday, December 14, 2015 4:46 AM
    Monday, December 14, 2015 2:05 AM

All replies

  • There are various ways of handling this; here's one way:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Font.Italic = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .OMaths.Count > 0 Then
          .End = .OMaths(1).Range.End
        Else
          .Font.Bold = True
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Monday, December 14, 2015 4:46 AM
    Monday, December 14, 2015 2:05 AM
  • Paul, it works. Thank you for helping me and probably other readers of your response. Question: When I use the C# the OMaths collection on Find object does not work. It gives me the error: Find does not contain a definition for OMaths.

    Update: My mistake, I was trying OMaths collection on Find object instead of Range object. It works with C# as well. Following is the C# equivalent of VBA's Do While Loop in mocropod's solution above. Here oRange = ActiveDocument.Range:

    while(oRange.Find.Found)
    {
       if (oRange.OMaths.Count > 0)
           oRange.End = oRange.OMaths[1].Range.End;
       else
           oRange.Font.Bold = -1;
           oRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
           oRange.Find.Execute();
    }

    Thanks..Nam




    • Edited by namwam Wednesday, December 16, 2015 4:32 PM
    Monday, December 14, 2015 4:46 AM
  • Sorry, I don't know C#, so I can't help you with that.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, December 14, 2015 10:57 PM
  • Paul, never mind, it was my mistake. I've added an Update section describing the C# version of your VBA's Do While Loop in my response above. Thanks..Nam
    Wednesday, December 16, 2015 3:18 PM