背景:我创建了一个Excel模板,通过邮件将字段合并到Word文档中,并生成5个不同的字母,发给一个客户。
使命:若要让Word VBA代码运行自动邮件合并,并提示使用源自邮件合并字段的文件名将其保存(或自动保存(到特定目录中。
即。
(唯一标识符(+首字母名称+日期保存在首字母文件夹中
(唯一标识符(+第二个字母的名称+日期保存在第二个信件文件夹中
等等。。
问题:我不知道如何指定目录,也不知道如何将邮件合并字段作为文件名的一部分插入。
以下是我有的代码
Sub MailMerge()
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
With Dialogs(wdDialogFileSummaryInfo)
.Title = "Letter1Draft" & Format(Now(), "mmddyyyy") & ".doc"
.Execute
End With
' Then this!
With Dialogs(wdDialogFileSaveAs)
.Show
End With
End Sub
下面的代码选择目录。它不允许您插入邮件合并字段作为文件名。
Sub AllSectionsToSubDoc()
Dim x As Long
Dim Sections As Long
Dim Doc As Document
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1
Doc.Sections(x).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "" & x & ".pdf")
ActiveDocument.Close False
Next x
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub