1.在Excel vba编辑器-工具-引用中添加对word.14的引用。
2.利用word vba查找替换功能替换word文本中文字。
'打开模板文件
Set objApp = CreateObject("Word.Application")
objApp.Visible = True
Set objDoc = objApp.Documents.Open(strTemplates, , False)
'开始替换模板预置变量文本
With objApp.Application.Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = "{$合同编号}"
.Replacement.Text = contact_NO
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{$甲方}"
.Replacement.Text = side_A
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{$乙方}"
.Replacement.Text = side_B
End With
.Find.Execute Replace:=wdReplaceAll
End With
'将写入数据的模板另存为文档文件
objDoc.SaveAs strFileName
objDoc.Saved = True
'MsgBox "合同文本生成完毕!", vbYes + vbExclamation
objDoc.Close
objApp.Quit
3.vba对象帮助网站。vba帮助网站