Quick question on sending emails via Excel

lopiteaux

Board Regular
Joined
Jun 8, 2011
Messages
77
Hi all,
I've got a little bit of code in place that creates an email from Excel. It's fairly basic, and I'd like to add the following three functions to it:

1) Add the current workbook as an attachment,

or (these do not necessarily need to work on the same message)

2) Add a predefined range as the MESSAGE BODY (i.e. File > Send To > Mail Recipient > Send the current sheet as the message body)

3) Add the signature stored in Outlook

4) Send it off.

This is the code I have sofar - as I said, real basic.

Code:
Sub SendEmail()
    Dim OutlookApp As Object, mail As Object
    Dim msgbody As String
    
    Set OutlookApp = CreateObject("Outlook.Application")
    
    msgbody = "Please find attached today's report." & vbCrLf & vbCrLf
    msgbody = msgbody & "Kind regards," & vbCrLf & vbCrLf
    
    Set mail = OutlookApp.CreateItem(0)
    With mail
        .SentOnBehalfOfName = "SENDER"
        .To = "RECIPIENT"
        .CC = "CCRECIPIENT"
        .Subject = "Report for COB " & Format(Date - 1, "dd/mm/yyyy")
        .body = msgbody
        .display
    End With
End Sub

Appreciate any help, thanks!

lopiteaux
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I'm not sure if this is useful to you but this is what Excel does if you use its built-in send by email option:

Application.Dialogs(xlDialogSendMail).Show
 
Upvote 0
Code:
sub email()
 
Dim outapp As Object
Dim outmail As Object
dim sBody as string
 
sBody = range("b3").value
 

Set outapp = CreateObject("Outlook.Application")
Set outmail = outapp.createitem(0)
With outmail
        .To = ""
         .CC = ""
        .Subject = sBody        
        .body = ""
        .Attachments.Add ""
        .display
        SendKeys "%{s}", True 
End With
Application.Wait (Now + TimeValue("00:00:05"))
SendKeys "%{s}", True 
Set outapp = Nothing
Set outmail = Nothing
 
Upvote 0
Thanks for the quick response TiaXL... I am able to add the attachment when a path is specified (in this case this works perfectly), so thanks a lot. It's pretty obvious, can't believe I didn't spot that one...

As to the signature and no. 2, sending it as the message body in Excel, any thoughts?

@moonfish, I've come across that method a few times but cannot seem to find the same functionality and flexibility that the method below gives me... are you able to add anything except a addressee and subject using your method (it IS much cleaner)?
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top