|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
谢谢楼主!不好意思,因水平太低,修改、添加、测试费了一个半小时,程序运行没有发现问题,样稿不大,但是是有手动换行符和不间断字符的文本,故意添加了一些半角、全角空格和制表符。有两点建议:一是统计结果最后不必出现提示消息框(及运行时间),这样给人以干净利落的感觉(现在代码也是喜欢能减一行就减一行,声明能在一行就在一行,行数太多显得程序太大);二是提取的汉字词语长度建议仅为二、三、四、五字词为宜,多了没有必要(类似:工作、办公室、与时俱进、民主集中制,再长就没有统计的必要了)。我略微修改的代码如下(只是提示信息和最后加了个表格,主体程序看不懂也未改动——顺便请解释一下我的那个帖子:正文四号14磅,则首行缩进为0.99厘米中的公式 2*14*2.54/72=0.99 每个数字是啥意思,3Q!):
Sub 词频统计()
Dim a As Byte, n As Long, filetext As String, d, Wd As Range, W As Range, b, c() As String, i As Long, temp As String
a = MsgBox("请选择——是:词频统计 否:字频统计", vbYesNo + vbExclamation, "中文字词频统计")
n = ActiveDocument.Content.ComputeStatistics(wdStatisticFarEastCharacters)
Set d = CreateObject("Scripting.Dictionary")
If a = vbYes Then
For Each Wd In ActiveDocument.Words
With Wd
If .Text Like "[一-龥]*" And Len(.Text) > 1 Then
If .Text Like "*[!一-龥]*" = False Then
d(.Text) = d(.Text) + 1
Else
For i = 1 To Len(.Text)
If Mid(.Text, i, 1) Like "[!一-龥]" Then Exit For
Next
With .Duplicate
.End = .Start + i - 1
For Each W In .Words
With W
If Len(.Text) > 1 Then
If Right(.Text, 1) Like "[!一-龥]" Then .End = .End - 1
d(.Text) = d(.Text) + 1
End If
End With
Next
End With
End If
End If
End With
Next
Else
filetext = ActiveDocument.Content.Text
For i = 1 To Len(filetext)
temp = Mid(filetext, i, 1)
If temp Like "[一-龥]" Then d(temp) = d(temp) + 1
Next
End If
b = d.keys
ReDim c(UBound(b))
For i = 0 To UBound(b)
c(i) = b(i) & vbTab & d(b(i))
Next
With Documents.Add.Content
.Text = IIf(a = 6, "词频统计", "字频统计") & "结果:文档共有" & n & "个中文字符,提取到" & d.Count _
& IIf(a = 6, "个中文词语", "个不同的汉字") & ",其出现次数分别为:" & vbCrLf & Join(c, vbCrLf)
.Parent.DefaultTabStop = .Characters.First.Font.Size * 6
.MoveStart wdParagraph
.Sort , 2, wdSortFieldNumeric, wdSortOrderDescending, 1, , , , , , wdSortSeparateByTabs
End With
ActiveDocument.Paragraphs(1).Range.Bold = True
ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(2).Range.Start, End:=ActiveDocument.Paragraphs.Last.Range.End).Select
Selection.ConvertToTable Separator:=wdSeparateByTabs
Selection.Tables(1).Style = "网格型"
Selection.HomeKey Unit:=wdStory
End Sub |
|