Sub GenerateWordDocuments()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim excelRow As Range
Dim templatePath As String
Dim outputPath As String
Dim fieldName As String
Dim fieldValue As String
Dim i As Integer
Dim lastRow As Integer
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
templatePath = "C:\Your\Template\Path\YourTemplate.docx" '替换为实际的模板路径
outputPath = "C:\Your\Output\Path\" '替换为实际的输出路径
Set wdApp = New Word.Application
wdApp.Visible = False
For Each excelRow In Sheet1.Range("A1:A" & lastRow).Rows
Set wdDoc = wdApp.Documents.Open(templatePath)
For i = 1 To excelRow.Cells.Count
fieldName = Sheet1.Cells(1, i).Value
fieldValue = excelRow.Cells(i).Value
wdDoc.Range.Replace What:="{" & fieldName & "}", Replacement:=fieldValue, LookAt:=wdFindWholeWord
Next i
wdDoc.SaveAs2 outputPath & excelRow.Cells(1).Value & ".docx" '使用第一列的值作为文件名
wdDoc.Close SaveChanges:=True
Next excelRow
wdApp.Quit
End Sub