2016-04-28 84 views
0

我的文档包含很多空格和段落标记。检测选择是否为Letter

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

回答

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 
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 

如果你想知道,如果(一个长度)的字符串是可以使用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 

编辑:移除的最后一个例子,因为它没有像它应该工作。

+0

的确如此,感谢您的评论。这就是我不会尝试每个角色的原因...... – arcadeprecinct