|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub Run()
Application.ScreenUpdating = False
'Word模板
Set WordApp = CreateObject("Word.Application")
TmpPath = ThisWorkbook.Path & "\模板.docx"
Set TmpDoc = WordApp.Documents.Open(Filename:=TmpPath, ReadOnly:=True)
'Excel数据表
Set DataSht = ThisWorkbook.Sheets(1)
DataLastRow = DataSht.Cells(Rows.Count, 1).End(xlUp).Row
DataArr = DataSht.Range("A1:AE" & DataLastRow)
DataCount = UBound(DataArr, 1)
'登记表所在文件夹
GenerateDir = ThisWorkbook.Path & "\合同生成文件夹"
'循环遍历每条登记信息,生成对应的Word准考证
For I = 2 To DataCount
' 将Word模板复制过来
TmpDoc.Content.Copy
Set NewDoc = WordApp.Documents.Add
NewDoc.Content.Paste
'往模板里填写登记信息
With NewDoc
.Bookmarks("原件编号").Range.Text = DataArr(I, 1)
End With
'保存并关闭新生成的Word文档
NewDoc.SaveAs2 GenerateDir & "\" & "技术开发合同-" & DataArr(I, 2) & ".docx"
NewDoc.Close
Next I
WordApp.Quit
Application.ScreenUpdating = True
MsgBox "合同生成完毕!" |
|