補充參考該文 在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