VBA获取WORD文档中各英文单词出现的次数,并输出

Sub 统计各单词出现的次数()
    Dim a As String, Dic As Object
    Dim myReg As Object, Matches As Object, Match As Object
    Dim k, i As Long, j As Long, temp As String, c As String
    
a = ActiveDocument.Content.Text
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
Set myReg = CreateObject("VBScript.RegExp")
With myReg
    .Pattern = "[A-Za-z]+"  '匹配模式:由纯字母组成的字符串
    .Global = True
    Set Matches = .Execute(a)
    For Each Match In Matches  '统计频次
        With Match
            If Dic.Exists(.Value) Then Dic(.Value) = Dic(.Value) + 1 Else Dic.Add .Value, 1
        End With
    Next
    k = Dic.Keys  '获取各“单词”
    For i = 0 To UBound(k) - 1 '排序
        For j = i + 1 To UBound(k)
            If k(i) > k(j) Then
                temp = k(i)
                k(i) = k(j)
                k(j) = temp
            End If
        Next
    Next
    For i = 0 To UBound(k)  '合并以用于输出
            c = c & k(i) & vbTab & Dic(k(i)) & Chr(13)
        Next
        Documents.Add.Content.Text = "共有如下" & Dic.Count & "个英文单词(含频次):" & Chr(13) & c
    End With
End Sub
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值