There must be a better way

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
 
J

Jonathan West

Hi Greg,

You can use the Like operator with the * wildcard in the comparison string,
like this

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 oWord Like "*[0-9]*" Then
oWord.MoveEnd Unit:=wdCharacter, Count:=-1
oWord.Font.Color = wdColorGreen
End If
ElseIf oWord Like "*[0-9]*" Then
oWord.Font.Color = wdColorRed
Else
oWord.Font.Color = wdColorAutomatic
End If
Next
End Sub

By the way, since your first ElseIf line is checking for whether oWord is
one of a number of single-character strings, you can use Instr for this. You
can replace that statement with the following

ElseIf Instr("-.,$", oWord.Text) > 0 Then

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup


Greg Maxey said:
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
 
G

Greg Maxey

Jonathan,

There was a better way. Thanks.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan said:
Hi Greg,

You can use the Like operator with the * wildcard in the comparison
string, like this

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 oWord Like "*[0-9]*" Then
oWord.MoveEnd Unit:=wdCharacter, Count:=-1
oWord.Font.Color = wdColorGreen
End If
ElseIf oWord Like "*[0-9]*" Then
oWord.Font.Color = wdColorRed
Else
oWord.Font.Color = wdColorAutomatic
End If
Next
End Sub

By the way, since your first ElseIf line is checking for whether
oWord is one of a number of single-character strings, you can use
Instr for this. You can replace that statement with the following

ElseIf Instr("-.,$", oWord.Text) > 0 Then


Greg Maxey said:
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
 
A

Andi Mayer

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:
this a my approches in an Access-project for the same problem.
Take attention to the result, they are not fitting what YOU want, but
it's easy to adopt

this is a function I use to strip non- Numericals

Achtung_es_gibt_Buchstaben is a public variable which is true if a
found a non-numerical

Public Function StripNoneNumerical(ByVal theString As String, _
Optional theLength As Long = 0) As Variant
Dim I, Nr As Long
Dim tmp As String
Achtung_es_gibt_Buchstaben = False
If theLength = 0 Then 'nur wenn ich nicht eine bestimmte Länge des
String durchsuchen will
theString = Trim(theString)
theLength = Len(theString)
End If

For I = 1 To theLength
Nr = Asc(Mid(theString, I, 1))
If (Nr >= 43 And Nr <= 57) And Nr <> 47 Then
'43="+" ;44=","; 45="-"; 46="."; 47="/"
tmp = tmp & Chr(Nr)
Else
Achtung_es_gibt_Buchstaben = True
End If
Next I
On Error Resume Next
StripNoneNumerical = Replace(tmp, ".", ",")
If Err.Number <> 0 Or StripNoneNumerical = "" Then StripNoneNumerical
= Null
On Error GoTo 0
End Function




this is a function I use for checking if the string is Numerical

Public Function isNoneNumerical(ByVal theNumber As Variant) As Boolean
Dim I, Nr As Long
For I = 1 To Len(theNumber)
Nr = Asc(Mid(theNumber, I, 1))
If (Nr >= 43 And Nr <= 57) And Nr <> 47 Then
'43="+" ;44=",";45="-"; 46="."; 47="/"
Else
isNoneNumerical = True
Exit Function
End If
Next I
End Function
 
G

Greg Maxey

Andi,

Thanks for your post. I have adapted some of your methods in my procedure.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Andi said:
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:
this a my approches in an Access-project for the same problem.
Take attention to the result, they are not fitting what YOU want, but
it's easy to adopt

this is a function I use to strip non- Numericals

Achtung_es_gibt_Buchstaben is a public variable which is true if a
found a non-numerical

Public Function StripNoneNumerical(ByVal theString As String, _
Optional theLength As Long = 0) As Variant
Dim I, Nr As Long
Dim tmp As String
Achtung_es_gibt_Buchstaben = False
If theLength = 0 Then 'nur wenn ich nicht eine bestimmte Länge des
String durchsuchen will
theString = Trim(theString)
theLength = Len(theString)
End If

For I = 1 To theLength
Nr = Asc(Mid(theString, I, 1))
If (Nr >= 43 And Nr <= 57) And Nr <> 47 Then
'43="+" ;44=","; 45="-"; 46="."; 47="/"
tmp = tmp & Chr(Nr)
Else
Achtung_es_gibt_Buchstaben = True
End If
Next I
On Error Resume Next
StripNoneNumerical = Replace(tmp, ".", ",")
If Err.Number <> 0 Or StripNoneNumerical = "" Then StripNoneNumerical
= Null
On Error GoTo 0
End Function




this is a function I use for checking if the string is Numerical

Public Function isNoneNumerical(ByVal theNumber As Variant) As Boolean
Dim I, Nr As Long
For I = 1 To Len(theNumber)
Nr = Asc(Mid(theNumber, I, 1))
If (Nr >= 43 And Nr <= 57) And Nr <> 47 Then
'43="+" ;44=",";45="-"; 46="."; 47="/"
Else
isNoneNumerical = True
Exit Function
End If
Next I
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top