word VBA在特定選中區域查找特定字符

補充參考該文 在WORD中用VBA實現光標移動與內容選擇

Private Function stringTatalNum(str As String) As Integer
'全文查找字符出現個數,並返回總數
 Dim n
  n = 0
    ActiveDocument.Range.Select
  Selection.Find.ClearFormatting
    With Selection.Find
        .Text = str
        .Forward = True
    End With
Do While True
        Selection.Find.Execute
        If Selection.Find.Found Then
           n = n + 1
        Else
            Exit Do
        End If
    Loop
    stringTatalNum = n
End Function



Private Function stringNumToEnd(str As String) As Integer
'選中光標到文末的內容,查找該區域出現的某個字符的次數
 Dim n, f
  n = 0
  '選中光標到文末的內容
 Selection.EndKey unit:=wdStory, Extend:=wdExtend
   Selection.Select
  Selection.Find.ClearFormatting
    With Selection.Find
        .Text = str
        .Forward = True
    End With
Do While True
        Selection.Find.Execute
        If Selection.Find.Found Then
           n = n + 1
        Else
            Exit Do
        End If
    Loop
    stringNumToEnd = n
End Function
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章