Word VBA 以合并字段作为文件名和提示保存在特定目录中
背景: 我创建了一个 Excel 模板,用于将字段通过邮件合并到 Word 文档中,并生成 5 封不同的信件,这些信件将发送给一位客户。
使命: 让 Word VBA 代码运行自动邮件合并并提示使用从邮件合并字段派生的文件名保存(或自动保存)在特定目录中。
IE。
(唯一标识符)+ 姓名首字母 + 日期 保存在第一个字母文件夹
(唯一标识符)+第二个字母的名称+日期 保存在第二封信文件夹
等中。
问题: 我不知道如何指定目录或如何插入邮件合并字段作为文件名的一部分。
以下是我的代码
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
Background:
I have created an Excel template to mail merge the fields into a Word document and generate 5 different letters which would go out to ONE customer.
Mission:
To have the Word VBA code run an automatic mail merge and prompt to save (or Autosave) in a specific directory with a file name which is derived from a mail merge field.
ie.
(unique identifier) + Name of First Letter + Date
to be saved in First Letter Folder
(unique identifier) + Name of Second Letter + Date
to be saved in Second Letter Folder
etc..
Issue:
I cannot figure out how to specify the directory or how to insert a mail merge field as a part of the file name.
The following is the code that I have
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码选择目录。
它不允许您插入邮件合并字段作为文件名。
The following code picks the directory.
It does not allow you to insert a mail merge field as the file name.