Attach files to an Outlook email using VBA

    The MailItem.Attachments Collection contains references to all of the attachments to a MailItem in Outlook. We can automate this in several ways to add attachments to an email.

Add attachment to every outbound email


    Add an attachment to every email you send by simply hooking the ItemSend Event and blindly adding the attachment.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Msg As Outlook.MailItem
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
 Set Msg = Item
Else
 Exit Sub
End If

Set MsgAttachments = Msg.Attachments
MsgAttachments.Add _
"path and filename of the file you want to attach to every email"

End Sub

Function IsMail(ByVal itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function

Add attachment based on email subject


    You might want to add attachments only to emails that contain a specific subject. In that case, an additional If statement in the above code will handle that.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Msg As Outlook.MailItem
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
  Set Msg = Item
Else
  Exit Sub
End If

If Instr(Msg.Subject, "your text") > 0 Then
  Set MsgAttachments = Msg.Attachments

  MsgAttachments.Add _
    "path and filename of the file you want to attach"
End If
End Sub

Function IsMail(ByVal itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function

Add attachment based on body text


    You might want to add attachments only to emails that have certain text in the body. For example, if you're talking about a certain project, you might want to attach the project plan, or an Excel workbook.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Msg As Outlook.MailItem
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
  Set Msg = Item
Else
  Exit Sub
End If

If Instr(Msg.Body, "your text") > 0 Then
  Set MsgAttachments = Msg.Attachments

  MsgAttachments.Add _
    "path and filename of the file you want to attach"
End If
End Sub

Function IsMail(ByVal itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function

Remove all attachments


    If you never want to send an attachment, loop through the Attachments Collection (backwards) and use the Delete Method. Note that this procedure may also remove images in your emails (like images in your signature), since they are considered part of the Attachments Collection.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Msg As Outlook.MailItem
Dim i As Long
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
  Set Msg = Item
Else
  Exit Sub
End If

Set MsgAttachments = Msg.Attachments

For i = MsgAttachments.Count to 1 Step -1
  MsgAttachments.Item(i).Delete
Next i

End Sub

Function IsMail(ByVal itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function
About Jimmy Peña

Just a regular guy who writes VBA for a living. I watch football and drink beer. I play guitar when my kids let me.

Comments

  1. carlos writes:

    SI quiero enviar varios archivos de una carpeta que empiecen por C que puedo utilizar en VBA. No encuentro nada.

  2. Preetesh writes:

    Can I get the code which can attach all the files available in a particular folder. However, everytime for each email there will be different path to fetch the files.

    For eg: Client A path to fetch the files will be: D:\Music\English\coldplay

    Client B path to fetch the files will be: D:\Music\English\Evanescence

  3. Nick Herrera writes:

    How to use the Add attachment to every outbound email option? I create the macro and change the path but nothing happens, what am I missing?

    Thanks.

    • Jimmy Peña writes:

      Copy and paste the code into the ThisOutlookSession module in Outlook's VBA editor. Restart Outlook and then send an email to test the code. Check your Sent Items folder to see if an attachment was added.

      There is no code to validate that the path and filename are valid, so you may want to add that as well.

  4. Achal writes:

    Hi,
    I am trying to bring up the add attachment option instead of adding a specific file, i want to give the user a chance to select the file they want to add.

    Thanks in advance for your help.

    Regards,
    Achal

  5. Ron Tonnet writes:

    Hi guys
    have tried the code here and it works fine. However i have to try and pick up multiple files always a set of different names from a directory (overwritten each fortnight). I have tried *.pdf with no success get filename error.

    Does anyone knw if it is possible to pick up all of the files at once in the directory or perhaps have it open at the directory folder and allow the users to select the files for insertion/attachment. the users have a real problem navigating dirctory structures. trying to kiss the function. Any help or views would be great.

  6. MOR writes:

    I want insert a Word.doc in an Outlook message as text using VBA. Can you help me?

  7. john writes:

    i am not erudite in using visual basic however i just got the software, how do i start a ThisOutlookSession to make the changes?

    • Jimmy Peña writes:

      ThisOutlookSession is a built-in module in Outlook's VBA Editor. If you are using Outlook then you already have it. You may have to install it, however, if VBA wasn't installed when you installed Outlook. It has nothing to do with Visual Basic per se.

Mentions:

  1. [...] I'll demonstrate the second way, since I've already posted code for the first way. (see Attach files to an Outlook email using VBA) Sub NewMsgWithAttachment() Const ATTACHMT As String = "C:My FilesAttachment.pdf" Dim [...]

Note: Comments are subject to the Blog Comment Policy and may not appear immediately. To post VBA code in your comment, use code tags like this: [vb]your code goes here[/vb]

Add a Comment:

*