Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

There must be a better way

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 13 Jan 2005 01:10 GMT
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

Signature

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

Jonathan West - 13 Jan 2005 10:56 GMT
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

Signature

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

> Hello,
>
[quoted text clipped - 38 lines]
> Next
> End Sub
Greg Maxey - 13 Jan 2005 11:36 GMT
Jonathan,

There was a better way.  Thanks.

Signature

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

> Hi Greg,
>
[quoted text clipped - 74 lines]
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support
Andi Mayer - 13 Jan 2005 12:10 GMT
>Hello,
>
[quoted text clipped - 10 lines]
>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
Greg Maxey - 14 Jan 2005 01:27 GMT
Andi,

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

Signature

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

>> Hello,
>>
[quoted text clipped - 75 lines]
> If you expect an answer to a personal mail, add the word "manfred" to
> the first 10 lines in the message MW
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.