【问题标题】:Detect if selection is Letter or not检测选择是否为字母
【发布时间】:2016-08-22 16:25:51
【问题描述】:

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

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

【问题讨论】:

    标签: vba ms-word word-2007 word-2003


    【解决方案1】:

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

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

    【讨论】:

    • 确实如此,感谢您的评论。这就是我没有尝试每个角色的结果......
    【解决方案2】:

    对段落标记做了一些研究,发现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
    

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多