检测选择是否为字母

Detect if selection is Letter or not

我的文档包含很多空格和段落标记。

我想做的是检测字符 Selection.find 是否是从 A 到 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

如果你想得到你选择的第一个字符,你也可以使用Left(Selection,1)。如果要查找第一个字母,可以使用:

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

如果您想知道一个字符串(长度为 1)是否是一个字母,您可以使用 Like 进行一些模式匹配:

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

或检查其 unicode 值

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

编辑:删除了最后一个示例,因为它没有正常工作。

对段落标记做了一些研究,发现 Chr(13) 可以检测 ^p(段落标记)。

以下代码可以检测paragraph markLetter

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