|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 批量读取word中的表格到excel()
- Dim arr(), brr(1 To 1, 1 To 15)
- Set wdApp = CreateObject("kwps.Application")
- wdApp.Visible = False ' 不显示Word应用程序
- On Error Resume Next
- Columns("N:N,P:P").NumberFormatLocal = "@"
- Set Rng = Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
- Set fd = Application.FileDialog(1)
- fd.AllowMultiSelect = True
- fd.Filters.Clear
- fd.Title = "选择word文件,可多选!"
- If fd.Show <> -1 Then MsgBox "未选择文件,退出程序!": Exit Sub
- For b = 1 To fd.SelectedItems.Count
- 路径 = fd.SelectedItems(b)
- If Right(路径, 3) = "doc" Or Right(路径, 4) = "docx" Then
- Set wdDoc = wdApp.Documents.Open(路径)
- Set wdTbl = wdDoc.Tables(1)
- ReDim arr(1 To wdTbl.Rows.Count, 1 To wdTbl.Columns.Count)
- brr(1, 1) = Application.Clean(wdTbl.Cell(3, 2).Range.Text)
- brr(1, 2) = Application.Clean(wdTbl.Cell(3, 4).Range.Text)
- brr(1, 4) = Application.Clean(wdTbl.Cell(5, 4).Range.Text)
- brr(1, 5) = Application.Clean(wdTbl.Cell(5, 2).Range.Text)
- brr(1, 6) = Application.Clean(wdTbl.Cell(6, 2).Range.Text)
- brr(1, 8) = Application.Clean(wdTbl.Cell(6, 4).Range.Text)
- brr(1, 9) = Mid(Application.Clean(wdTbl.Cell(11, 2).Range.Text), 15, 10000)
- brr(1, 13) = Application.Clean(wdTbl.Cell(8, 4).Range.Text)
- brr(1, 15) = Application.Clean(wdTbl.Cell(7, 6).Range.Text)
- Rng.Resize(1, 15) = brr
- Set Rng = Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
- Erase arr
- wdDoc.Close SaveChanges:=False
- End If
- Next b
- wdApp.Quit
- MsgBox "完成!"
- End Sub
复制代码 |
|