Search code examples
vbaoutlook

How to create a mailitem?


I'm trying to send the active Excel workbook as an attachment via Outlook.

Whenever I run the code it says

Invalid use of New key word

at New Outlook.MailItem`.

Sub SendOutlook()
    'Declaring Variables
    Dim OutlookApp  As Outlook.Application
    Dim OutlookEmail  As Outlook.MailItem

    'Assigning variables to create outlook application and mailitem
    Set OutlookApp = New Outlook.Application
    Set OutlookEmail = New Outlook.MailItem
    
    With OutlookEmail
        'Format of the mail
        .BodyFormat = olFormatPlain
        'Body of the mail
        .Body = "Dear Someone" & vbNewLine & "How are you?"
        'To whom you want to send mail
        .To = "[email protected]"
        'Subject of mail
        .Subject = "Write Subject Here"
        'TO add an attachment
        .Attachments.Add ActiveWorkbook.FullName
        'sends the mail
        .Send
    End With
    
End Sub

Solution

  • You cannot create a MailItem via New. It must be created using CreateItem of the the Outlook Application Object.

       Set OutlookApp = New Outlook.Application
       Set OutlookEmail = OutlookApp.CreateItem(olMailItem)