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

你可能感兴趣的:(VBA开发)