|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
做一个空模板word放在同文件夹下面,模板.doc
空了几行,体力活儿了,自己填上吧,试了一下,
- Private Sub aa()
- Dim Wordapp As New Word.Application, path, fname, fullname, i, j
- path = ThisWorkbook.path
- lastrow = Sheets("21年").Range("B65536").End(xlUp).Row
- s = 0
- For i = 3 To lastrow - 1
- fname = "三、HS-AQBD-079 事故调查报告 "
- FileCopy path & "\模板.doc", path & "" & fname & "(" & Sheets("21年").Range("A" & i) & ").doc"
- fullname = path & "" & fname & "(" & Sheets("21年").Range("A" & i) & ").doc"
- With Wordapp
- .Documents.Open fullname
- .Visible = False
- .ActiveDocument.Tables(1).Cell(1, 2).Range = Sheets("21年").Cells(i, 1)
- .ActiveDocument.Tables(1).Cell(1, 4).Range = Sheets("21年").Cells(i, 6)
- .ActiveDocument.Tables(1).Cell(1, 6).Range = Sheets("21年").Cells(i, 7)
- .ActiveDocument.Tables(1).Cell(3, 2).Range = Sheets("21年").Cells(i, 3)
-
-
-
-
-
-
-
- End With
- Wordapp.Documents.Save
- Wordapp.Quit
- Set Wordapp = Nothing
- Next i
- If s = 0 Then
- MsgBox "已输出到 Word 文件!"
- End If
- End Sub
复制代码
运行正常
|
|