Create Outlook Emails with PDF Attachments Grouped by Filename
Question details
Create one Outlook email per person by scanning a folder and grouping PDF attachments according to a name or prefix in each filename.
- Product
- Microsoft Outlook
- Device & OS
- not provided
- Scenario
- A folder contains PDFs for multiple recipients, and each recipient should receive one email containing all of their matching files.
- Observed behavior
- The desired result is a separate email for each recipient, with the correct PDFs attached and the recipient and subject populated.
Use classic Outlook for Windows; new Outlook and Outlook for Mac do not support this Outlook VBA workflow. For the example below, place the PDFs in one folder and name them using recipient@example.com__document-name.pdf, with two underscores separating the recipient address from the document name.
Group PDF files by recipient and create Outlook emails with VBA
Use this macro when each PDF filename starts with the recipient's email address followed by two underscores. It opens one email per recipient for review without automatically sending anything.
The macro uses a dictionary to collect PDF paths under each recipient address. It scans only the selected folder, not its subfolders, and skips filenames that do not match the expected pattern.
If filenames contain names or customer IDs instead of email addresses, add a verified name-to-email lookup before assigning the recipient. A person's name alone is not a reliable email address.
In File Explorer, place the files in one folder and use names such as alex@example.com__invoice.pdf and alex@example.com__statement.pdf. Both files will be attached to one email addressed to alex@example.com. Use a small test folder first.
In classic Outlook for Windows, press Alt+F11 to open the Visual Basic editor, then choose Insert > Module. If your organization blocks VBA macros, ask your administrator about an approved automation method rather than bypassing the restriction.
Paste this code into the new module: Option Explicit Public Sub CreateGroupedPdfEmails() Dim picker As Object, selectedFolder As Object Dim fso As Object, folder As Object, file As Object Dim groups As Object, paths As Collection Dim folderPath As String, stem As String, recipient As String Dim separatorPos As Long, skipped As Long, created As Long Dim key As Variant, filePath As Variant Dim mail As Outlook.MailItem On Error GoTo Failed Set picker = CreateObject("Shell.Application") Set selectedFolder = picker.BrowseForFolder(0, "Select the folder containing PDFs", 0) If selectedFolder Is Nothing Then Exit Sub folderPath = selectedFolder.Self.Path Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(folderPath) Then MsgBox "Select a local or accessible network folder.", vbExclamation Exit Sub End If Set folder = fso.GetFolder(folderPath) Set groups = CreateObject("Scripting.Dictionary") groups.CompareMode = vbTextCompare For Each file In folder.Files If LCase$(fso.GetExtensionName(file.Name)) = "pdf" Then stem = fso.GetBaseName(file.Name) separatorPos = InStr(1, stem, "__", vbBinaryCompare) recipient = "" If separatorPos > 1 Then recipient = Trim$(Left$(stem, separatorPos - 1)) End If If InStr(1, recipient, "@", vbBinaryCompare) > 1 And _ InStr(1, recipient, " ", vbBinaryCompare) = 0 And _ InStr(1, recipient, ";", vbBinaryCompare) = 0 And _ InStr(1, recipient, ",", vbBinaryCompare) = 0 Then If Not groups.Exists(recipient) Then Set paths = New Collection groups.Add recipient, paths End If Set paths = groups.Item(recipient) paths.Add file.Path Else skipped = skipped + 1 End If End If Next file For Each key In groups.Keys Set mail = Application.CreateItem(olMailItem) mail.To = CStr(key) mail.Subject = "Your PDF documents" mail.Body = "Hello," & vbCrLf & vbCrLf & _ "Please find your PDF documents attached." Set paths = groups.Item(key) For Each filePath In paths mail.Attachments.Add CStr(filePath) Next filePath mail.Display created = created + 1 Next key MsgBox "Emails opened: " & created & vbCrLf & _ "PDFs skipped for unmatched filenames: " & skipped, vbInformation Exit Sub Failed: MsgBox "Stopped: " & Err.Description & vbCrLf & _ "Review any emails already opened before running again.", vbExclamation End Sub
Place the cursor inside CreateGroupedPdfEmails and press F5 in the VBA editor. Select the PDF folder in the folder picker and click OK. Outlook opens one message window for each recipient found in the filenames.
In each opened Outlook message, check the To field, subject, and every attachment before clicking Send. Use Save or Ctrl+S if you want to keep the message in Drafts for later. Re-running the macro creates new messages, so close or remove unwanted duplicates first.
Prepare PDF attachments with WPS Office before emailing them
WPS Office is a free, lightweight alternative for everyday document work, with a familiar interface and Microsoft Office format compatibility for a smooth transition. Use it to open and review documents and PDFs before attaching them; it does not run Outlook VBA or automate Outlook email grouping.

Frequently Asked Questions
Can I run this PDF attachment macro in new Outlook?
No. New Outlook does not support Outlook VBA, and Outlook for Mac does not support this workflow either. Open classic Outlook for Windows and press Alt+F11 to use the macro, subject to your organization's security policy.
What if my PDF filenames start with a person's name instead of an email address?
Add a lookup that maps each filename prefix to a verified email address before setting mail.To. For example, map Alex Smith to alex@example.com. The provided macro expects recipient@example.com__document-name.pdf and will skip name-only prefixes.
Will the macro send the emails automatically?
No. The mail.Display line opens each email for review. Click Send in each message after checking the recipient and attachments. Although VBA can use mail.Send, unattended sending should only be considered after validating the recipient mapping and testing the workflow.
Why were some PDFs not attached?
Check the completion message for skipped files, then inspect their names in File Explorer. Each filename must include an email prefix followed by two underscores. The macro processes only files with a .pdf extension in the selected folder; it does not scan subfolders.
What should I do if one recipient's attachments are too large?
Review the combined attachment size in the opened Outlook message and compare it with your mail provider's limits. Remove oversized attachments and share them through an approved cloud-storage link, or split them into separate messages if your organization's policy allows it.




