Consulting

Results 1 to 7 of 7

Thread: Highlighting all numbers in a word document

  1. #1

    Highlighting all numbers in a word document

    Hello,

    I want to highlight all numbers in a word document (for which I have written a macro) but I don't want to highlight the number(s) if there is a M or MC before the number(s).
    This is my current code:

    Sub Highlight()


    Selection.Find.ClearFormatting
    Selection.Find.Highlight = False
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    With Selection.Find
    .Text = "([0-9])"
    .Replacement.Text = "\1"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    End With
    Selection.Find.Execute
    Selection.Find.Execute Replace:=wdReplaceAll



    End Sub

    How I can ignore the M[0-9] and MC[0-9]?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I'm sorry, but your macro doesn't highlight anything.

    Sub Highlight()
      Dim oRng As Word.Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "[0-9]{1,}"
        .MatchWildcards = True
        While .Execute
          Select Case oRng.Start
            Case 0
              oRng.HighlightColorIndex = wdBrightGreen
            Case 1
              If Not oRng.Characters.First.Previous.Text Like "M" Then
                oRng.HighlightColorIndex = wdBrightGreen
              End If
            Case Else
              If Not oRng.Characters.First.Previous.Text & oRng.Characters.First.Previous.Previous.Text Like "MC" Then
                If Not oRng.Characters.First.Previous.Text Like "M" Then
                  oRng.HighlightColorIndex = wdBrightGreen
                End If
              End If
          End Select
        Wend
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Hello,

    thank you so much for your help. My macro actually finds and replaces the numbers. While replacing the numbers, it highlights them.
    This works but I cannot exclude the M"numbers" and "MC"numbers. Your macro seems to be perfect but I have a runtime error 4641 in the last oRng.HighlightColorIndex = wdBrightGreen line. It says that the Highlight Color order is deactivated. I don't know what this means.

  4. #4
    Ok, I solved the Problem. But there is another Problem. It still highlights the MC numbers. The M numbers are not highlighted but the MC numbers.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub Highlight()
        Dim oRng As Word.Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Text = "[0-9]{1,}"
            .MatchWildcards = True
            While .Execute
                Select Case oRng.Start
                Case 0
                    oRng.HighlightColorIndex = wdBrightGreen
                Case 1
                    If Not oRng.Characters.First.Previous.Text Like "M" Then
                        oRng.HighlightColorIndex = wdBrightGreen
                    End If
                Case Else
                    If Not oRng.Characters.First.Previous.Text & oRng.Characters.First.Previous.Previous.Text Like "MC" Then
                        If Not oRng.Characters.First.Previous.Text Like "[C,M]" Then
                            oRng.HighlightColorIndex = wdBrightGreen
                        End If
                    End If
                End Select
            Wend
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    This is great, thank you

  7. #7
    Don't forget to mark your thread as solved This was an interesting post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •