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

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

Re: There must be a better way by Jonathan

Jonathan
Thu Jan 13 04:56:32 CST 2005

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" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:%23tv2cyQ%23EHA.3120@TK2MSFTNGP12.phx.gbl...
> 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
>
> --
> Greg Maxey/Word MVP
> A Peer in Peer to Peer Support
>


Re: There must be a better way by Greg

Greg
Thu Jan 13 05:36:45 CST 2005

Jonathan,

There was a better way. Thanks.

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

Jonathan West wrote:
> 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" <gmaxey@mvps.OscarRomeoGolf> wrote in message
> news:%23tv2cyQ%23EHA.3120@TK2MSFTNGP12.phx.gbl...
>> 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
>>
>> --
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support



Re: There must be a better way by Andi

Andi
Thu Jan 13 06:10:30 CST 2005

On Wed, 12 Jan 2005 20:10:07 -0500, "Greg Maxey"
<gmaxey@mvps.OscarRomeoGolf> wrote:

>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

---
If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW

Re: There must be a better way by Greg

Greg
Thu Jan 13 19:27:51 CST 2005

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 Mayer wrote:
> On Wed, 12 Jan 2005 20:10:07 -0500, "Greg Maxey"
> <gmaxey@mvps.OscarRomeoGolf> wrote:
>
>> 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
>
> ---
> If you expect an answer to a personal mail, add the word "manfred" to
> the first 10 lines in the message MW