Detect if selection is Letter or not

7.4k Views Asked by At

My document contains lot of blank spaces and paragraph marks.

What I want to do is detect if the Character Selection.find is any letter from A to Z?

Dim EE As String

Selection.Find.ClearFormatting
With Selection.Find
    .Text = "^?"
    .Forward = True
    .Wrap = wdFindStop
End With

Selection.Find.Execute

EE = Selection.Text

If isletter = True Then
    MsgBox ("Letter found")
Else
    MsgBox ("No letter found")
End If
3

There are 3 best solutions below

0
On BEST ANSWER

Had done some research on paragraph mark and find out that Chr(13) can detect ^p(paragraph mark).

following code can detect paragraph mark or Letter.

Sub FindanyLetter()
Dim EE As String

        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "^?"
            .Forward = False
            .Wrap = wdFindStop
        End With
        Selection.Find.Execute

        EE = Selection.Text

        If EE = Chr(13) Or EE = " " Then

        MsgBox "Paraghraph mark or space"
        Else
        MsgBox "Letter"

        End If

    End Sub
1
On

If you want to get the first character of your selection you can also use Left(Selection,1). If you want to find the first letter, you can use:

With Selection.Find
    .MatchWildcards = true
    .Text = "[a-zA-Z]"  '[A-Z] if you only want upper case
    .Forward = True
    .Wrap = wdFindStop
End With

If you want to know if a string (of length one) is a letter you can use Like to do some pattern matching:

If EE Like "[A-Z]" Then   '"[a-zA-Z]" for upper and lower case

or check its unicode value

If AscW(EE)>=AscW("A") and AscW(EE)<=AscW("Z") Then  'for upper case letters

edit: removed last example because it didn't work as it should.

0
On

I used to convert selection as string and pass it to this method

Function IsLetter(c As String) As Boolean
    If UCase(c) <> LCase(c) Then
        IsLetter = True
    Else
        IsLetter = False
    End If
End Function