|
下面是代码:
VBA代码:
Sub myMailMerge()
Dim myMerge As MailMerge, i As Integer, myname As String
Application.ScreenUpdating = False
Set myMerge = ActiveDocument.MailMerge
With myMerge.DataSource
If .Parent.State = wdMainAndDataSource Then
.ActiveRecord = wdFirstRecord
For i = 1 To .RecordCount
.FirstRecord = i
.LastRecord = i
.Parent.Destination = wdSendToNewDocument
'生成的各文档的文件名,以数据源第1个和第2个字段的当前数据命名,请自行修改命名公式,例 "公司文档" & .DataFields (4)
myname = .DataFields(2).Value & “ ” & .DataFields(1).Value
.ActiveRecord = wdNextRecord
.Parent.Execute
With ActiveDocument
.Content.Characters.Last.Previous.Delete
'生成的各文档保存于D盘根目录下,请自行修改文档保存的路径
.SaveAs "D:\" & myname & ".doc"
.Close
End With
Next
End If
End With
Application.ScreenUpdating = True
End Sub |
|