0

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

我想要做的是检测字符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
4

2 回答 2

3

如果你想获得你选择的第一个字符,你也可以使用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

编辑:删除了最后一个示例,因为它没有按应有的方式工作。

于 2016-04-28T06:45:46.627 回答
0

对段落标记做了一些研究,发现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
于 2016-04-28T15:47:38.060 回答