Skip to: Site menu | Main content

Login

Name: 
Password:
Remember me?
Register

Automating Outlook to Send an Email with an Attachment

written by Mark Rowlinson - Last updated Sep 2005

The following code will send an email to name@host.com with the attachement C:\test.txt attached. Please note that in versions of Outlook higher than 2000 you will get security warnings that a program is trying to control Outlook. This unfortunately can reduce it's use.

 
 
Sub SendMail() 
     
    Dim olApp As Outlook.Application 
    Dim olMail As Outlook.MailItem 
    Dim blRunning As Boolean 
     
     'get application
    blRunning=True 
    On Error Resume Next 
    Set olApp = GetObject(, "Outlook.Application") 
    If olApp Is Nothing Then 
        Set olApp = New Outlook.Application 
        blRunning=False 
    End If 
    On Error Goto 0 
     
    Set olMail = olApp.CreateItem(olMailItem) 
    With olMail 
         'Specify the email subject
        .Subject = "My email with attachment" 
         'Specify who it should be sent to
         'Repeat this line to add further recipients
        .Recipients.Add "name@host.com" 
         'specify the file to attach
         'repeat this line to add further attachments
        .Attachments.Add "c:test.txt" 
         'specify the text to appear in the email
        .Body = "Here is an email" 
         'Choose which of the following 2 lines to have commented out
        .Display 'This will display the message for you to check and send yourself
         '.Send ' This will send the message straight away
    End With 
     
    If Not blRunning Then olApp.Quit 
     
    Set olApp=Nothing 
    Set olMail=Nothing 
     
End Sub