Outlook Vba to create Email with Attachment

VBA code to create Outlook email

This code will create a new email item in Outlook. 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, enter corresponding contents HTML code.

Automate Email with Outlook VBA

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
    
    'Define data type for Mail item required parameters
    Dim sTo As String
    Dim sSubject As String
    Dim sMailContent As String
    Dim sFilePath1 As String, sFilepath2 As String
    
    'Initialize all objects & parameters
    Set oOutlookApp = Outlook.Application
    Set oMailItem = oOutlookApp.CreateItem(olMailItem)
    sTo = "ToEmail@domain.com"
    sSubject = "Subject goes here"
    sMailContent =  "Mail Content in HTML" 
    sFilePath1 = ""
    sFilepath2 = ""
    
    'Assign values to Mail item & send email
    With oMailItem
        .To = sTo
        .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
        .Send
    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. Only 2 file are attached as example, But any number of files can be attached using this VBA.