Outlook VBA To Create Email & Attach files

This article has tested working code in Outlook vba to send email with attachment automatically to any recipients email address. Using this code, You can automate creating emails in Outlook application. Also send emails automatically with attachment and customized content.

In this code, the mail subject & content can be customized. The mail body can be either a plain text or HTML content.

If it is HTML content, you have to input the actual HTML code of the content. This kind of HTML content is preferred if your email has media objects like images or video files. Also, with HTML email, you can create more appealing beautiful emails.

VBA Send Email in Outlook with Attachment

Now, lets see the code.

If you are looking to create email from Outlook application, this code can be used and no reference need to be added.

To send email from Excel vba, click here.

Sub Outlook_Send_Email_VBA()
    'Define data type for Outlook mail objects - officetricks
    Dim oOutlookApp As Outlook.Application
    Dim oMailItem As Outlook.MailItem
    Dim sMailContent As String, sFilePath1 As String, sFilepath2 As String
    'Initialize all objects & parameters
    Set oOutlookApp = New Outlook.Application
    Set oMailItem = oOutlookApp.CreateItem(olMailItem)
    sMailContent =  "Mail Content in HTML" 
    sFilePath1 = ""
    sFilepath2 = ""
    'Assign values to Mail item & send email
    With oMailItem
        .To = "ToEmail@domain.com"
        .Subject = "Email subject"
        .BodyFormat = olFormatHTML
        .HTMLBody = sMailContent & .HTMLBody
        '.Attachments.Add sFilePath1 <- Provide path of file1
        '.Attachments.Add sFilepath2 <- Provide path of file2
        '.Display <- To view created mail on screen before sending
    End With
    'Process Completed
    MsgBox "Mail Sent"
End Sub

Before sending any email using this code, enter proper path for the file. Also make sure the attachment file exists in the path.

This code creates a new Outlook application, creates a new email item, and sets the recipient, subject, body, and attachment. It then sends the email. You can change the recipient, subject, body, and attachment path to suit your needs.

Only 2 file are attached as example, But any number of files can be attached using this VBA.

Running the VBA Code to Send Outlook Email

Once you have written the VBA code, you can run it by clicking the “Run” button in the VBA editor or by pressing “F5” on your keyboard. This will send the email with attachments.

It’s also possible to assign the code to a button or a shortcut, so you can easily run it whenever you need to send an email with attachments. You can also use the code as part of a larger macro that performs multiple tasks in Outlook.

Related Topics that might interest you

  1. Send Outlook Email at specific time using VBA
  2. Remove Signature from Outlook email using VBA Code.