|
发表于 2011-7-25 22:15
|
显示全部楼层
本楼为最佳答案
Option Explicit
Sub yy()
Dim dpath As String
Dim Filename As String
Dim v As Integer
Dim n As String
Dim w As Integer
Dim i As Long
Dim d As String
Dim k As Long
Dim wdapp As Object
Dim dic As Object
Dim rngs As String
Dim wddct As Object
Set dic = CreateObject("scripting.dictionary")
On Error Resume Next
dpath = ThisWorkbook.Path & "\数据箱"
Set wdapp = CreateObject("Word.Application")
'wdapp.Visible = True
Set wddct = CreateObject("Word.Document")
Application.ScreenUpdating = False
Filename = Dir(dpath & "\*.doc")
ReDim arr(1 To 1000, 1 To 3)
Do While Filename <> ""
Set wddct = wdapp.Documents.Open(dpath & "\" & Filename)
v = wddct.Paragraphs.Count
n = Left(LTrim(wddct.Range(Start:=wddct.Paragraphs(1).Range.Start, End:=wddct.Paragraphs(1).Range.End)), 4)
For w = 2 To v
rngs = wddct.Range(Start:=wddct.Paragraphs(w).Range.Start, End:=wddct.Paragraphs(w).Range.End)
For i = 0 To UBound(Split(rngs, " "))
If Split(rngs, " ")(i) <> "" Then
d = d & Replace(Split(rngs, " ")(i), vbCr, "")
If Len(d) >= 2 Then
If Not dic.Exists(d) Then
k = k + 1
dic(d) = k
arr(dic(d), 1) = d
End If
If arr(dic(d), 2) <> "" Then arr(dic(d), 2) = "'" & arr(dic(d), 2) & ","
arr(dic(d), 2) = arr(dic(d), 2) & n
arr(dic(d), 3) = arr(dic(d), 3) + 1
d = ""
End If
End If
Next
Next
wddct.Close
Filename = Dir()
Loop
Range("e2").Resize(k, 3) = arr
Set wddct = Nothing
wdapp.Quit
Set wdapp = Nothing
Set dic = Nothing
Application.ScreenUpdating = True
End Sub |
|