- 新建一个excel到word同级目录
- alt+f11打开vba窗口并新建模块
- 粘贴下方代码(修改一些必要参数)
- 回到excel表格界面,alt+f8选择执行该宏
- 注意要在信任中心开启运行vba宏
Sub 批量提取word表格数据到excel()Dim wdApp As Object, wdDoc As ObjectDim fso As Object, folder As Object, file As ObjectDim excelRow As Long, iRow As Long, iCol As IntegerDim tableNo As IntegerDim folderPath As String tableNo = 1 excelRow = 1 folderPath = ActiveWorkbook.Path & "\" Set fso = CreateObject("Scripting.FileSystemObject")Set folder = fso.GetFolder(folderPath)On Error Resume NextSet wdApp = GetObject(, "Word.Application")If Err.Number <> 0 ThenSet wdApp = CreateObject("Word.Application")End IfOn Error GoTo 0wdApp.Visible = False For Each file In folder.FilesIf (fso.GetExtensionName(file.Path) = "doc") Or (fso.GetExtensionName(file.Path) = "docx") ThenSet wdDoc = wdApp.Documents.Open(file.Path)If wdDoc.Tables.Count >= tableNo ThenWith wdDoc.Tables(tableNo)For iRow = 5 To 5For iCol = 2 To 2Cells(excelRow, iCol - 1).Value = WorksheetFunction.Clean(Replace(.Cell(iRow, iCol).Range.Text, vbCr, ""))Next iColexcelRow = excelRow + 1Next iRowFor iRow = 3 To 3For iCol = 5 To 5Cells(excelRow - 1, iCol - 3).Value = WorksheetFunction.Clean(Replace(.Cell(iRow, iCol).Range.Text, vbCr, ""))Next iColexcelRow = excelRowNext iRowEnd WithEnd IfwdDoc.Close SaveChanges:=FalseEnd IfNext filewdApp.QuitSet wdDoc = NothingSet wdApp = NothingSet fso = NothingMsgBox "提取完毕!找到文件数量:" & folder.Files.Count-2
End Sub