2016-04-28 88 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