' 宣告一个新的 Word 应用对象
' 要先引用:Microsoft Word x.xx Library
' 调用实例:GenerateDocument "<Name>,<Sex>", "张三|男", "C:\MyDot.dot", "C:\NewFile.doc"
Private Function GenerateDocument(sTags, sValues, sSourcePath, sDestPath) As Boolean
Dim wdApp As New Word.Application
On Local Error GoTo ErrHandler
Dim arrTags() As String, arrValues() As String, iLoop As Integer
wdApp.Documents.Open sSourcePath
arrTags = Split(sTags, ",")
arrValues = Split(sValues, "|")
With wdApp.ActiveDocument.Content.Find
For iLoop = 0 To UBound(arrTags)
.Execute arrTags(iLoop), , True, , , , , , , arrValues(iLoop), 2
Next iLoop
End With
'退出和释放之前关闭这个word文档对象:
If Len(Dir(sDestPath)) > 0 Then
Kill sDestPath
End If
wdApp.ActiveDocument.SaveAs sDestPath
wdApp.ActiveDocument.Close
wdApp.Quit
Set wdApp = Nothing