需求
现要求将一个 Excel 数据表中的每行数据导成一个 Word 文档,即有多少行数据就生成多少个 Word 文档,Excel 每列与 Word 文档中的表格项一一对应。
实现
前置工作:将 Word 文档空表格当作模板文档做好,与 Excel 数据源文件置于同一路径下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| Sub 分离() Application.ScreenUpdating = False p = ThisWorkbook.Path & "/" f = p & "空白模板.doc" Dim myWS As Worksheet Set myWS = ThisWorkbook.Sheets(1) For i = 3 To 54 FileCopy f, p & "test/" & myWS.Cells(i, 2).Text & ".doc" Set wd = CreateObject("word.application") Set d = wd.documents.Open(p & "test/" & myWS.Cells(i, 2).Text & ".doc") d.tables(1).Cell(1, 2) = myWS.Cells(i, 2).Text d.tables(1).Cell(5, 4) = myWS.Cells(i, 20).Text d.Close wd.Quit Set wd = Nothing Next Application.ScreenUpdating = True End Sub
|