2
votes

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
1

1 Answers

0
votes

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