G
Greg Maxey
Hello,
I started out trying to develop a simple test to determine if each word in a
document was numeric, alphanumeric, or plain letter text. The beast just
kept growing. Numeric or non numeric was simple. It got complicated with
the alphanumeric text as I couldn't find a simple comparison. I tried a
Like statement "[A-z], but apparently there is no {1,} to continue looking
for one or more. I next shifted to a Instr test, but I couldn't figure out
how to write a statement Instr(oWord, *) where * represents any number 0-9.
Finally I settled on a series Instr statements in an Or construction. The
next glitch was numbers like 1-800-867-5309, 1,200, 12.23 etc.
I added a few other tests to handle those with the below macro. If someone
knows of a better way to test for an alphanumeric number, please let me
know:
Sub Alphanumeric()
Dim oWord As Word.Range
For Each oWord In ActiveDocument.Words
If IsNumeric(oWord) Then
oWord.Font.Color = wdColorGreen
ElseIf oWord.Text = "-" Or oWord.Text = "." _
Or oWord.Text = "," Or oWord.Text = "$" Then
oWord.MoveEnd Unit:=wdCharacter, Count:=1
If InStr(oWord, 0) Or InStr(oWord, 1) Or InStr(oWord, 2) _
Or InStr(oWord, 3) Or InStr(oWord, 4) Or InStr(oWord, 5) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Or InStr(oWord, 8) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Then
oWord.MoveEnd Unit:=wdCharacter, Count:=-1
oWord.Font.Color = wdColorGreen
End If
ElseIf InStr(oWord, 0) Or InStr(oWord, 1) Or InStr(oWord, 2) _
Or InStr(oWord, 3) Or InStr(oWord, 4) Or InStr(oWord, 5) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Or InStr(oWord, 8) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Then
oWord.Font.Color = wdColorRed
Else: oWord.Font.Color = wdColorAutomatic
End If
Next
End Sub
I started out trying to develop a simple test to determine if each word in a
document was numeric, alphanumeric, or plain letter text. The beast just
kept growing. Numeric or non numeric was simple. It got complicated with
the alphanumeric text as I couldn't find a simple comparison. I tried a
Like statement "[A-z], but apparently there is no {1,} to continue looking
for one or more. I next shifted to a Instr test, but I couldn't figure out
how to write a statement Instr(oWord, *) where * represents any number 0-9.
Finally I settled on a series Instr statements in an Or construction. The
next glitch was numbers like 1-800-867-5309, 1,200, 12.23 etc.
I added a few other tests to handle those with the below macro. If someone
knows of a better way to test for an alphanumeric number, please let me
know:
Sub Alphanumeric()
Dim oWord As Word.Range
For Each oWord In ActiveDocument.Words
If IsNumeric(oWord) Then
oWord.Font.Color = wdColorGreen
ElseIf oWord.Text = "-" Or oWord.Text = "." _
Or oWord.Text = "," Or oWord.Text = "$" Then
oWord.MoveEnd Unit:=wdCharacter, Count:=1
If InStr(oWord, 0) Or InStr(oWord, 1) Or InStr(oWord, 2) _
Or InStr(oWord, 3) Or InStr(oWord, 4) Or InStr(oWord, 5) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Or InStr(oWord, 8) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Then
oWord.MoveEnd Unit:=wdCharacter, Count:=-1
oWord.Font.Color = wdColorGreen
End If
ElseIf InStr(oWord, 0) Or InStr(oWord, 1) Or InStr(oWord, 2) _
Or InStr(oWord, 3) Or InStr(oWord, 4) Or InStr(oWord, 5) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Or InStr(oWord, 8) _
Or InStr(oWord, 6) Or InStr(oWord, 7) Then
oWord.Font.Color = wdColorRed
Else: oWord.Font.Color = wdColorAutomatic
End If
Next
End Sub