Skip to content

Instantly share code, notes, and snippets.

@wecsam
Created July 12, 2018 18:16
Show Gist options
  • Save wecsam/a971782ef3adbe1d5e2d4edbf0459711 to your computer and use it in GitHub Desktop.
Save wecsam/a971782ef3adbe1d5e2d4edbf0459711 to your computer and use it in GitHub Desktop.
Attach selected files to selected Outlook items
' This macro is for Microsoft Outlook. It opens a file picker dialog and then
' attaches the selected files to the selected items.
' This macro requires the object library for the installed version of Microsoft
' Word. In the VBA editor, go to Tools > References... and make sure that it is
' checked off. For Word 2016, choose the Microsoft Word 16.0 Object Library.
Sub AddAttachmentsToSelection()
Dim objMessage As Variant
Dim strPath As Variant
' Outlook does not have a file picker. Use an instance of Word instead.
Dim appWord As Word.Application
Set appWord = New Word.Application
appWord.Visible = False
On Error GoTo cleanup
' Create the file picker.
Dim fdPicker As FileDialog
Set fdPicker = appWord.Application.FileDialog(msoFileDialogFilePicker)
fdPicker.Title = "Attach Files"
fdPicker.ButtonName = "Attach"
' Show the file picker.
If fdPicker.Show = -1 Then
' Add each file as an attachment to each message in the selection.
For Each objMessage In Application.ActiveExplorer.Selection
For Each strPath In fdPicker.SelectedItems
objMessage.Item(i).Attachments.Add strPath, olByValue
objMessage.Save
Next strPath
Next objMessage
End If
cleanup:
' Close Word.
appWord.Quit
Set appWord = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment