|
楼主 |
发表于 2016-5-4 16:20
|
显示全部楼层
Private Sub CommandButton1_Click() '批量提取Word的数据到表格
Dim WordApp As Object, DOC, mTable, Fn$, Str$
On Error Resume Next
CreateObject("wscript.shell").Run "cmd.exe /c dir """ & ThisWorkbook.Path & "\*.docx"" /s/b>""" & ThisWorkbook.Path & "\list.txt""", False, True '取得指定目录下的word文档清单
Set WordApp = CreateObject("word.application") '创建word程序项目(用于操作word文档)
WordApp.Visible = True '设定word程序项目可见
Open ThisWorkbook.Path & "\list.txt" For Input As #1 '打开清单文件并读取内容
ReDim arr(1 To 10, 1 To 5)
WordApp.ScreenUpdating = False
While Not EOF(1) '循环读取清单文件各行内容
Input #1, Str '输入一行文本到变量str中
If Trim(Str) <> "" Then '如果文本有效则
Set DOC = WordApp.documents.Open(Trim(Str)) '利用word程序项目打开对应的word文档
With DOC
n = n + 1
arr(n, 1) = Trim(Mid(.Sentences(2), 6, 10)) '第2句的6---15字符 '.Range(Start:=38, End:=46))总字符数
arr(n, 2) = Replace(Replace(.Sentences(15), Chr(7), ""), Chr(13), "")
arr(n, 3) = Replace(Replace(.Sentences(16), Chr(7), ""), Chr(13), "")
arr(n, 4) = Replace(Replace(.Sentences(17), Chr(7), ""), Chr(13), "")
arr(n, 5) = Replace(Replace(.Sentences(18), Chr(7), ""), Chr(13), "")
.Close False '关闭word文档
End With
End If
Wend
Close #1 '关闭清单文件
If Dir(ThisWorkbook.Path & "\list.txt") <> "" Then Kill ThisWorkbook.Path & "\list.txt" '删除清单文件
WordApp.Quit 'word程序项目关闭
Set DOC = Nothing '清空对应项目变量
Set WordApp = Nothing
WordApp.ScreenUpdating = True
[a2].Resize(10, 5) = arr
End Sub |
|