Group: microsoft.public.word.vba.general
From: Jay Freedman
Date: Monday, February 25, 2008 8:35 PM
Subject: Re: ISASCII function

On Mon, 25 Feb 2008 14:28:11 -0600, Charlie Mac <> wrote:

>VBA Gerus,
>
>Is there a function in VBA to derermine if a character is an ASCII
>character (A - Z or a - z)? I need something like this: msgbox
>IsASCII(aWord) returning a one or zero.
>
>Thanks.
>
>Charlie from Texas

To correct a misconception, ASCII (now called ANSI) characters include much more
than A-Z and a-z -- the digits, punctuation, and all other characters in the
range from 0 to 255 are 'ASCII characters'. So I wrote the function name as
IsAlpha instead.

Function IsAlpha(s As String) As Boolean
Dim retVal As Boolean
retVal = False

If Len(s) > 0 Then
If Left$(s, 1) Like "[A-Za-z]" Then
retVal = True
End If
End If

IsAlpha = retVal
End Function

Sub test()
Dim myText As String
Dim myChar As String
Dim msg As String
Dim i As Long

myText = Selection.Text
For i = 1 To Len(myText)
myChar = Mid$(myText, i, 1)
msg = "'" & myChar & "' is "
If Not IsAlpha(myChar) Then
msg = msg & "NOT "
End If
msg = msg & "in [A-Za-z]"
MsgBox msg
Next
End Sub

As written, this function checks only one character, the first one in the string
passed in as the parameter. If you meant to check a whole word or an entire
string, that would need some slightly different logic.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.