hmltnangel
Active Member
- Joined
- Aug 25, 2010
- Messages
- 290
- Office Version
- 365
- Platform
- Windows
Hi All,
I have been creating individual docs to be issued by email. Whist the volumes were small I never bothered looking to see if VBA could make the Merge auto send the emails too. So, can I do this in VBA too.
Essentially my mail merge will create (using the below VBA) a bunch of individually named docs, these doc need to go to the relevant named person.
I have been creating individual docs to be issued by email. Whist the volumes were small I never bothered looking to see if VBA could make the Merge auto send the emails too. So, can I do this in VBA too.
Essentially my mail merge will create (using the below VBA) a bunch of individually named docs, these doc need to go to the relevant named person.
VBA Code:
Sub Merge_To_Individual_Files()
' Sourced from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Set MainDoc = ActiveDocument
With MainDoc
StrFolder = .Path & "\"
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
On Error Resume Next
For i = 1 To .DataSource.RecordCount
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
If Trim(.DataFields("Last_Name")) = "" Then Exit For
'StrFolder = .DataFields("Folder") & "\"
StrName = .DataFields("Last_Name") & ", " & .DataFields("First_Name")
End With
On Error GoTo NextRecord
.Execute Pause:=False
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next
StrName = Trim(StrName)
With ActiveDocument
'Add the name to the footer
'.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore StrName
.SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
NextRecord:
Next i
End With
End With
Application.ScreenUpdating = True
End Sub