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