web 2.0


Attaching files to Outlook email item using VBA

Ever wondered how to attach a file to an Outlook mail item using VBA? This is a nice little piece of code that will do just that without too much complication.

Remember, however, that you will need to install the references to Outlook so that it will workd. This code was tested in Outlook 2007, 2003 and 2002. Enjoy!

Sub Example()
  Dim oOutlook As Outlook.Application
  Dim oEmailItem As MailItem

  On Error Resume Next
  Set oOutlook = GetObject(, "Outlook.Application")
  If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application")

  Set oEmailItem = oOutlook.CreateItem(olMailItem)

  With oEmailItem
    .Attachments.Add "C:\MyFile.doc"
    .To = "email@email.com"
    .Display
  End With

  Set oEmailItem = Nothing
  Set oOutlook = Nothing

End Sub

Tags: , , ,

Microsoft Outlook | Microsoft Outlook - VBA

Comments are closed