I am trying to automate creating a mail merge in Word from an Excel spreadsheet. I recorded a macro that does most of it, but the recording doesn't work when the 'Edit Recipients' dialog is open, so I need to go into the VBA code to do the sorting and filtering. Currently the entire code is this:
Sub Castingdirectors()
'
' Castingdirectors Macro
'
'
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
ChangeFileOpenDirectory "Y:\--file path redacted--\CASTING BOOK\"
ActiveDocument.SaveAs2 FileName:= _
"Y:\--file path redacted--\Casting Directors.docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
ActiveWindow.Close
End Sub
This generates a mail merge of all entries.
I need to filter so that any entries where the field 'CASTING DIRECTOR SORT' is blank are NOT included, and to sort by CASTING DIRECTOR SORT ascending.
I could do this by inserting a 'skip record' field into the body of the Word mail merge doc and doing the 'sort' manually, but I am trying to create a 'master' mail merge document, with several macros assigned to command buttons that will execute and save different mail merges with different sorting/filtering.