Sub 文件詞頻() '由文件字頻改來'2015/11/28
Dim d As Document, Char, charText As String, preChar As String _
, x() As String, xT() As Long, i As Long, j As Long, ds As Date, de As Date '
'Dim ExcelSheet As New Excel.Worksheet 'As Object,
Dim xlApp As Excel.Application, xlBook As Excel.Workbook, xlSheet As Excel.Worksheet
Dim ReadingLayoutB As Boolean
Static Ln
Dim xlsp As String
On Error GoTo ErrH:
Set d = ActiveDocument
'If xlsp = "" Then xlsp = 取得桌面路徑 & "" 'GetDeskDir() & ""
'If Dir(xlsp) = "" Then xlsp = 取得桌面路徑 'GetDeskDir
'xlsp = InputBox("請輸入存檔路徑及檔名(全檔名,含副檔名)!" & vbCr & vbCr & _
"預設將以此word文件檔名 + ""詞頻.XLSX""字綴,存於桌面上", "詞頻調查", xlsp & Replace(d.Name, ".doc", "") & "詞頻" & StrConv(Time, vbWide) & ".XLSX")
'If xlsp = "" Then Exit Sub
xlsp = 取得桌面路徑 & "" & Replace(d.Name, ".doc", "") & "_詞頻" & StrConv(Time, vbWide) & ".XLSX"
If Ln = "" Then Ln = 1
Ln = InputBox("請指定詞彙長度" & vbCr & vbCr & "檔案會存在桌面上名為:" & vbCr & vbCr & Replace(d.Name, ".doc", "") & "_詞頻" & StrConv(Time, vbWide) & ".XLSX" & _
vbCr & vbCr & "的檔案", , Ln + 1)
If Ln = "" Then Exit Sub
If Not IsNumeric(Ln) Then Exit Sub
If Ln > 11 Or Ln < 2 Then Exit Sub
ds = VBA.Timer
With d
For Each Char In d.Characters
Select Case Ln
Case 2
charText = Char & Char.Next
Case 3
charText = Char & Char.Next & Char.Next.Next
Case 4
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next
Case 5
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next
Case 6
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next
Case 7
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next
Case 8
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next
Case 9
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next
Case 10
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next.Next
Case 11
charText = Char & Char.Next & Char.Next.Next & Char.Next.Next.Next & Char.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next.Next & Char.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next
End Select
If Not charText Like "*[-' 。,、;:?:,;,〈〉《》 ''「」『』()▽△?!()【】—""()<>" _
& ChrW(9312) & Chr(-24153) & Chr(-24152) & ChrW(8218) & Chr(13) & Chr(10) & Chr(11) & ChrW(12) & Chr(63) & Chr(9) & Chr(-24152) & Chr(-24153) & "▽□】【~/︵—]*" _
And Not charText Like "*[a-zA-Z0-90-9]*" And InStr(charText, ChrW(-243)) = 0 And InStr(charText, Chr(91)) = 0 And InStr(charText, Chr(93)) = 0 Then
'If Not charText Like "[a-z1-9]" & Chr(-24153) & Chr(-24152) & " 、'""「」『』()-?!]" Then
' If InStr(Chr(-24153) & Chr(-24152) & Chr(2) & "‧[]〔〕﹝﹞…;,,.。. 、'""‘’`\{}{}「」『』()《》〈〉-?!]", charText) = 0 Then
If Not charText Like "*[" & ChrW(-24153) & ChrW(-24152) & Chr(2) & "‧[]〔〕﹝﹞…;,,.。. 、'""‘’`\{}{}「」『』()《》〈〉-?!‘{}]*" Then
'chr(2)可能是註腳標記
If preChar <> charText Then
'If UBound(X) > 0 Then
If preChar = "" Then 'If IsEmpty(X) Then'如果是一開始
GoTo 1
ElseIf UBound(Filter(x, charText)) Then ' <> charText Then '如果尚無此字
1 ReDim Preserve x(i)
ReDim Preserve xT(i)
x(i) = charText
xT(i) = xT(i) + 1
i = i + 1
Else
GoSub 詞頻加一
End If
'End If
Else
GoSub 詞頻加一
End If
preChar = charText
End If
End If
Next
End With
12
Dim Doc As New Document, Xsort() As String, U As Long ', xTsort() As Integer, k As Long, so As Long, ww As String
If U = 0 Then U = 1 '若無執行「詞頻加一:」副程序,若無超過1次的詞頻,則 Xsort(xT(j - 1)) = Xsort(xT(j - 1)) & "、" & x(j - 1) & _
會出錯:陣列索引超出範圍 2015/11/5
ReDim Xsort(U) As String
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
With xlSheet.Application
For j = 1 To i
.Cells(j, 1) = x(j - 1)
.Cells(j, 2) = xT(j - 1)
Xsort(xT(j - 1)) = Xsort(xT(j - 1)) & "、" & x(j - 1) 'Xsort(xT(j - 1)) & ww '陣列排序'2010/10/29
Next j
End With
Doc.ActiveWindow.Visible = False
If d.ActiveWindow.View.ReadingLayout Then ReadingLayoutB = True: d.ActiveWindow.View.ReadingLayout = Not d.ActiveWindow.View.ReadingLayout
'U = UBound(Xsort)
For j = U To 0 Step -1 '陣列排序'2010/10/29
If Xsort(j) <> "" Then
With Doc
If Len(.Range) = 1 Then '尚未輸入內容
.Range.InsertAfter "詞頻 = " & j & "次:(" & Len(Replace(Xsort(j), "、", "")) / Ln & "個)"
.Range.Paragraphs(1).Range.Font.Size = 12
.Range.Paragraphs(.Paragraphs.Count).Range.Font.Name = "新細明體"
.Range.Paragraphs(.Paragraphs.Count).Range.Font.NameAscii = "Times New Roman"
'.Range.Paragraphs(1).Range.Font.Bold = True
Else
.Range.InsertParagraphAfter
.ActiveWindow.Selection.Range.Collapse Direction:=wdCollapseEnd
.Range.InsertAfter "詞頻 = " & j & "次:(" & Len(Replace(Xsort(j), "、", "")) / Ln & "個)"
.Range.Paragraphs(.Paragraphs.Count).Range.Font.Size = 12
'.Range.Paragraphs(.Paragraphs.Count).Range.Bold = True
.Range.Paragraphs(.Paragraphs.Count).Range.Font.Name = "新細明體"
.Range.Paragraphs(.Paragraphs.Count).Range.Font.NameAscii = "Times New Roman"
End If
.Range.InsertParagraphAfter
.ActiveWindow.Selection.Range.Collapse Direction:=wdCollapseEnd
.Range.Paragraphs(.Paragraphs.Count).Range.Font.Size = 12
' .Range.Paragraphs(.Paragraphs.Count).Range.Bold = False
.Range.InsertAfter Replace(Xsort(j), "、", Chr(9), 1, 1) 'chr(9)為定位字元(Tab鍵值)
.Range.InsertParagraphAfter
If InStr(.Range.Paragraphs(.Paragraphs.Count).Range, "詞頻") = 0 Then
.Range.Paragraphs(.Paragraphs.Count - 1).Range.Font.Name = "標楷體"
Else
.Range.Paragraphs(.Paragraphs.Count).Range.Font.Name = "新細明體"
.Range.Paragraphs(.Paragraphs.Count).Range.Font.NameAscii = "Times New Roman"
End If
End With
End If
Next j
With Doc.Paragraphs(1).Range
.InsertParagraphBefore
.Font.NameAscii = "times new roman"
Doc.Paragraphs(1).Range.InsertParagraphAfter
Doc.Paragraphs(1).Range.InsertParagraphAfter
Doc.Paragraphs(1).Range.InsertAfter "你提供的文本共使用了" & i & "個不同的詞彙(傳統字與簡化字不予合併)"
End With
Doc.ActiveWindow.Visible = True
de = VBA.Timer
Doc.SaveAs Replace(xlsp, "XLS", "doc") '分大小寫
If ReadingLayoutB Then d.ActiveWindow.View.ReadingLayout = Not d.ActiveWindow.View.ReadingLayout
Set d = Nothing ' ActiveDocument.Close wdDoNotSaveChanges
Debug.Print Now
MsgBox "完成!" & vbCr & vbCr & "費時" & Left(de - ds, 5) & "秒!", vbInformation
xlSheet.Application.Visible = True
xlSheet.Application.UserControl = True
xlSheet.SaveAs xlsp
Exit Sub
詞頻加一:
For j = 0 To UBound(x)
If x(j) = charText Then
xT(j) = xT(j) + 1
If U < xT(j) Then U = xT(j) '記下最高詞頻,以便排序(將欲排序之陣列最高元素值設為此,則不會超出陣列.
'多此一行因為要重複判斷計算好幾次,故效能不增反減''效能還是差不多啦.
Exit For
End If
Next j
Return
ErrH:
Select Case Err.Number
Case 4605 '閱讀模式不能編輯'此方法或屬性無法使用,因為此命令無法在閱讀中使用。
' If ActiveWindow.View.SplitSpecial = wdPaneNone Then
' ActiveWindow.ActivePane.View.Type = wdNormalView
' Else
' ActiveWindow.View.Type = wdNormalView
' End If
' If ActiveWindow.View.SplitSpecial = wdPaneNone Then
' ActiveWindow.ActivePane.View.Type = wdPrintView
' Else
' ActiveWindow.View.Type = wdPrintView
' End If
'Doc.Application.ActiveWindow.View.ReadingLayout
d.ActiveWindow.View.ReadingLayout = False ' Not d.ActiveWindow.View.ReadingLayout
Doc.ActiveWindow.View.ReadingLayout = False
Doc.ActiveWindow.Visible = False
ReadingLayoutB = True
Resume
Case 91, 5941 '沒有設定物件變數或 With 區塊變數,集合中所需的成員不存在
GoTo 12
Case Else
MsgBox Err.Number & Err.Description, vbCritical 'STOP: Resume
Resume
End
End Select
End Sub