Word VBA 以合并字段作为文件名和提示保存在特定目录中

发布于 2024-11-30 14:54:27 字数 817 浏览 2 评论 0原文

背景: 我创建了一个 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2024-12-07 14:54:27

以下代码选择目录。
它不允许您插入邮件合并字段作为文件名。

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

The following code picks the directory.
It does not allow you to insert a mail merge field as the file name.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文