PDA

View Full Version : [SOLVED:] Highlighting all numbers in a word document



Cinema
11-27-2015, 05:05 AM
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]?

gmaxey
11-28-2015, 04:55 PM
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

Cinema
11-30-2015, 01:55 AM
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.

Cinema
11-30-2015, 03:40 AM
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.

gmaxey
11-30-2015, 05:03 AM
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

Cinema
11-30-2015, 12:21 PM
This is great, thank you :D

MacroWizard
11-30-2015, 07:59 PM
Don't forget to mark your thread as solved :) This was an interesting post.