简体   繁体   中英

Send Outlook email with attachment using Excel VBA

I am trying to send email with attachment using Excel VBA.

My code is sending email if I am not including the attachment code line.

When I write that line to send an attachment it is showing error.

Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)
With MyItem
    .To = "saurabh.ad.sharma@accenture.com"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "resport"
    .Attachment = "C:\Users\saurabh.ad.sharma\Desktop\rrr.xlsx"
End With
MyItem.Send

A simple example to send mail with attachment try this out

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
'comment the next line if you do not want to see the outlook window
objMailItem.Display
strEmailAddr  = "me.me@you.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = "Hi"
objMailItem.Attachments.Add "file.xml"
objMailItem.Send
Set objMailItem = nothing
Set objOutl = nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM