Function readWord() Dim vFiles As Variant '定义文件类型 If VarType(vFiles) = vbBoolean Then ExitFunction EndIf Dim wordApp AsObject OnErrorResumeNext Set wordApp = GetObject(, "word.application") '调用WORD程序 Set wordApp = CreateObject("word.application") '如果当前WORD程序未打开,则打开WORD应用程序 Dim doc AsObject Dim bFound AsBoolean Dim vFile As Variant ForEach vFile In vFiles Application.CutCopyMode = False Application.ScreenUpdating = False Set doc = wordApp.documents.Open(vFile) bFound = False Dim tbl AsObject ForEach tbl In doc.Tables If Left(tbl.Rows(1).Cells(2).Range.Text, 3) = "优化前"Then bFound = True tbl.Range.Copy With ThisWorkbook.Worksheets.Add(after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)) curLogCell.Offset(0, 1).Value = .Name ThisWorkbook.Worksheets(SHEET_NAME_FIRST).Activate OnErrorResumeNext .Cells(1, 1).PasteSpecial Paste:=xlPasteValues EndWith EndIf Next If bFound = FalseThen MsgBox "没找到对应表" curLogCell.Offset(0, 2).Value = curLogCell.Offset(0, 2).Value & "/" & "No Table" EndIf doc.Close Application.ScreenUpdating = True Next wordApp.Quit EndFunction
第2步:将上述生成的不同工作表按照一定要求合并到同一张工作表中;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Function combine() Dim iCNT AsInteger Dim destSht As Worksheet Set destSht = Workbooks.Add.Worksheets(1) Dim srcSht As Worksheet OnErrorResumeNext ForEach srcSht In ThisWorkbook.Worksheets If srcSht.Rows(3).Cells(1) = ""Then srcSht.Rows(3).Delete srcSht.Columns("A:A").Copy Destination:=destSht.Cells(1, 1) srcSht.Columns("B:C").Copy _ Destination:=destSht.Cells(1, destSht.Columns.Count).End(xlToLeft).Offset(0, 1) Else srcSht.Columns("A:A").Copy Destination:=destSht.Cells(1, 1) srcSht.Columns("B:C").Copy _ Destination:=destSht.Cells(1, destSht.Columns.Count).End(xlToLeft).Offset(0, 1) EndIf Next EndFunction