A Simple Send Email Procedure That Works

sdruley

Well-known Member
Joined
Oct 3, 2010
Messages
557
Office Version
  1. 365
Platform
  1. Windows
Apparently, the technology for sending emails from Excel vba is in a state of flux, particularly, when you consider that the syntax seems to be different for each version of Excel or each version of your operating system.
Does anyone know of a simple approach for Excel 360, 64 bit and windows 10?
My goal is to take the contents of one cell on a worksheet, assign a date and time stamp and email it to my brother

Date _____Time _______ T
3/4/2019, 12:32:02 AM, -1 with no quotes

I am in to my 3rd day on this problem. Anyone's assistance would be greatly appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Howdy

I dealt with a similar problem for another user earlier today.

I'm not clear as to how your data is laid out, so am going to assume Date and Time are in two different cells.


  1. Is "-1 with no quotes" part of the info to go in the email?
  2. Could you please provide a simple layout of the table holding your data (with grid ref's) showing where the following are located:
    • Recipient's email address
    • Date
    • Time
    • Other
  3. What goes in the following "fields" of the email:
    • Subject?
    • Message body?
  4. Where in the email do you want the date and time to be placed?
 
Upvote 0
Howdy

I dealt with a similar problem for another user earlier today.

I'm not clear as to how your data is laid out, so am going to assume Date and Time are in two different cells.


  1. Is "-1 with no quotes" part of the info to go in the email?
  2. Could you please provide a simple layout of the table holding your data (with grid ref's) showing where the following are located:
    • Recipient's email address
    • Date
    • Time
    • Other
  3. What goes in the following "fields" of the email:
    • Subject?
    • Message body?
  4. Where in the email do you want the date and time to be placed?

SEND TO: Const = xxx
SENDER: Const = yyy
COPIED: Const = zzz
SUBJECT: Rainbow 2
BODY: 3/4/2019, 6:30:04 AM, -1 {Now()}, {Now()}, {-1,1,9}
REFERENCE/PLACEMENT: New Sheet @ $A$1, {-1,1,9} = $A$6

Would try sending to yourself to see if code works
 
Upvote 0
Col Delane,

Forgot to thank you for your efforts to work on this. I am very grateful.
 
Upvote 0
I was trying to build a non-VBA solution using just the HYPERLINK function but can't quite get the message body to work properly so am adapting some old code I had in my Excel toolbox that sends an email via Outlook.

A couple more clarifications:
1. Are the Date and Time values required the current (i.e. Now ) values of each at the time the email is sent?
2. I'm not familiar with the following elements of the BODY - could you please explain? (i.e. should this text be sent as is, or are they formula arguments that should return values?)
-1 {Now()}, {Now()}, {-1,1,9}​
REPLACEMENT New Sheet @ $A$1, {-1,1,9} = $A$6
 
Upvote 0
I was trying to build a non-VBA solution using just the HYPERLINK function but can't quite get the message body to work properly so am adapting some old code I had in my Excel toolbox that sends an email via Outlook.

A couple more clarifications:
1. Are the Date and Time values required the current (i.e. Now ) values of each at the time the email is sent?
2. I'm not familiar with the following elements of the BODY - could you please explain? (i.e. should this text be sent as is, or are they formula arguments that should return values?)
-1 {Now()}, {Now()}, {-1,1,9}​
REPLACEMENT New Sheet @ $A$1, {-1,1,9} = $A$6

{Now()} refers to the vba code formatted as the date and then the time
{-1,1,9} refers to the possible solutions in the set
$A$6 is a typo
 
Upvote 0
Try this:

I'm still not completely clear about the content of the Message Body - but you should be able to modify those two lines/cells as you desire.

Spreadsheet:
[TABLE="class: head"]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[TH][/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[/TH]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
[/TD]
[TD]Email Field[/TD]
[TD]
Current Data
[/TD]
[TD]
Cell Content
[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
[/TD]
[TD]Send To:[/TD]
[TD]fastlane@tpg.com.au[/TD]
[TD]<< Data[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
[/TD]
[TD]Sender:[/TD]
[TD]As per mail client[/TD]
[TD]<< Data[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
[/TD]
[TD]Copy To:[/TD]
[TD]col.delane@tpg.com.au[/TD]
[TD]<< Data[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
[/TD]
[TD]Subject line text:[/TD]
[TD]Rainbow 2[/TD]
[TD]<< Data[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
[/TD]
[TD]Message body text:[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]​
[/TD]
[TD] Line 1:[/TD]
[TD]03/07/19, 07:16:12 PM[/TD]
[TD]<< Formula[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]8[/COLOR]​
[/TD]
[TD] Line 2:[/TD]
[TD]Reference/Placement: New Sheet @ $A$1, {-1,1,9}[/TD]
[TD]<< Data[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]9[/COLOR]​
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[/TABLE]

Formulas:
[TABLE="class: head"]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[TH][/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]
[/TH]
[TH]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]
[/TH]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]
[/TD]
[TD] Line 1:[/TD]
[TD]= TEXT( NOW(), "mm/dd/yy, hh:mm:ss AM/PM" )[/TD]
[TD]<< Formula[/TD]
[/TR]
[/TABLE]
I'm from down-under (Australia), so just check that NOW() and the TEXT formula returns the correct value and format for the date for your region.

Defined Names:
[TABLE="class: head"]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD]BodyLine1[/TD]
[TD]='.'!$B$7[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD]BodyLine2[/TD]
[TD]='.'!$B$8[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD]CopyToEmailAddress[/TD]
[TD]='.'!$B$4[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD]SendToEmailAddress[/TD]
[TD]='.'!$B$2[/TD]
[/TR]
[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] "]
[TD]Subject[/TD]
[TD]='.'!$B$5[/TD]
[/TR]
[/TABLE]

VBA Code:

  1. Copy this code to a standard module in the Visual Basic Editor
  2. Add a button (Form Control) to the spreadsheet, or to your Quick Access Toolbar, and link it to the macro.
Code:
Option Explicit

Sub mail_text_html()

    Dim OutApp As Object
    Dim OutMail As Object
    
    Dim strbody As String
    
    Dim EmailSendTo As String
    Dim EmailCopyTo As String
    Dim EmailSubject As String
    Dim myRange As Range
    Dim EmailBody1 As String
    Dim EmailBody2 As String
    
    Set myRange = Range("SendToEmailAddress")
    EmailSendTo = myRange.Text
    
    Set myRange = Range("CopyToEmailAddress")
    EmailCopyTo = myRange.Text
    
    Set myRange = Range("Subject")
    EmailSubject = myRange.Text
    
    Set myRange = Range("BodyLine1")
    EmailBody1 = myRange.Text
    
    Set myRange = Range("BodyLine2")
    EmailBody2 = myRange.Text


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
    With OutMail
        .Subject = EmailSubject
        .To = EmailSendTo
        .CC = EmailCopyTo
        .Body = EmailBody1 & vbNewLine & EmailBody2
        
        Select Case MsgBox("Click:" & vbLf & vbLf & "Yes to DISPLAY the message (then manually send from email client)," & vbLf & vbLf & _
            "No to SEND the message directly without displaying, or" & vbLf & vbLf & "Cancel to abort.", vbYesNoCancel Or vbQuestion Or vbDefaultButton1, "E-mail Despatch")
            
            Case vbYes
                    .Display
            Case vbNo
                    .Send
                    MsgBox "Done."
            Case vbCancel
                    Exit Sub
                    
        End Select
                
        End With

On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub


Post back with any issues / what you think.
 
Upvote 0
Try this:

I'm still not completely clear about the content of the Message Body - but you should be able to modify those two lines/cells as you desire.

Spreadsheet:
[TABLE="class: head"]
[TR]
[TH][/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]A[/COLOR]
[/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]B[/COLOR]​
[/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]C[/COLOR]​
[/TH]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]1[/COLOR]​
[/TD]
[TD]Email Field[/TD]
[TD]
Current Data
[/TD]
[TD]
Cell Content
[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]2[/COLOR]​
[/TD]
[TD]Send To:[/TD]
[TD]fastlane@tpg.com.au[/TD]
[TD]<< Data[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]3[/COLOR]​
[/TD]
[TD]Sender:[/TD]
[TD]As per mail client[/TD]
[TD]<< Data[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]4[/COLOR]​
[/TD]
[TD]Copy To:[/TD]
[TD]col.delane@tpg.com.au[/TD]
[TD]<< Data[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]5[/COLOR]​
[/TD]
[TD]Subject line text:[/TD]
[TD]Rainbow 2[/TD]
[TD]<< Data[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]6[/COLOR]​
[/TD]
[TD]Message body text:[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]7[/COLOR]​
[/TD]
[TD] Line 1:[/TD]
[TD]03/07/19, 07:16:12 PM[/TD]
[TD]<< Formula[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]8[/COLOR]​
[/TD]
[TD] Line 2:[/TD]
[TD]Reference/Placement: New Sheet @ $A$1, {-1,1,9}[/TD]
[TD]<< Data[/TD]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]9[/COLOR]​
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[/TABLE]

Formulas:
[TABLE="class: head"]
[TR]
[TH][/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]A[/COLOR]​
[/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]B[/COLOR]
[/TH]
[TH]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]C[/COLOR]
[/TH]
[/TR]
[TR]
[TD]
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]7[/COLOR]
[/TD]
[TD] Line 1:[/TD]
[TD]= TEXT( NOW(), "mm/dd/yy, hh:mm:ss AM/PM" )[/TD]
[TD]<< Formula[/TD]
[/TR]
[/TABLE]
I'm from down-under (Australia), so just check that NOW() and the TEXT formula returns the correct value and format for the date for your region.

Defined Names:
[TABLE="class: head"]
[TR]
[TD]BodyLine1[/TD]
[TD]='.'!$B$7[/TD]
[/TR]
[TR]
[TD]BodyLine2[/TD]
[TD]='.'!$B$8[/TD]
[/TR]
[TR]
[TD]CopyToEmailAddress[/TD]
[TD]='.'!$B$4[/TD]
[/TR]
[TR]
[TD]SendToEmailAddress[/TD]
[TD]='.'!$B$2[/TD]
[/TR]
[TR]
[TD]Subject[/TD]
[TD]='.'!$B$5[/TD]
[/TR]
[/TABLE]

VBA Code:

  1. Copy this code to a standard module in the Visual Basic Editor
  2. Add a button (Form Control) to the spreadsheet, or to your Quick Access Toolbar, and link it to the macro.
Code:
Option Explicit

Sub mail_text_html()

    Dim OutApp As Object
    Dim OutMail As Object
    
    Dim strbody As String
    
    Dim EmailSendTo As String
    Dim EmailCopyTo As String
    Dim EmailSubject As String
    Dim myRange As Range
    Dim EmailBody1 As String
    Dim EmailBody2 As String
    
    Set myRange = Range("SendToEmailAddress")
    EmailSendTo = myRange.Text
    
    Set myRange = Range("CopyToEmailAddress")
    EmailCopyTo = myRange.Text
    
    Set myRange = Range("Subject")
    EmailSubject = myRange.Text
    
    Set myRange = Range("BodyLine1")
    EmailBody1 = myRange.Text
    
    Set myRange = Range("BodyLine2")
    EmailBody2 = myRange.Text


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
    With OutMail
        .Subject = EmailSubject
        .To = EmailSendTo
        .CC = EmailCopyTo
        .Body = EmailBody1 & vbNewLine & EmailBody2
        
        Select Case MsgBox("Click:" & vbLf & vbLf & "Yes to DISPLAY the message (then manually send from email client)," & vbLf & vbLf & _
            "No to SEND the message directly without displaying, or" & vbLf & vbLf & "Cancel to abort.", vbYesNoCancel Or vbQuestion Or vbDefaultButton1, "E-mail Despatch")
            
            Case vbYes
                    .Display
            Case vbNo
                    .Send
                    MsgBox "Done."
            Case vbCancel
                    Exit Sub
                    
        End Select
                
        End With

On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub


Post back with any issues / what you think.

This is a masterpiece of instruction aimed at the worksheet, range names, formulas, error algorithms and vba code that brings all of the intellectual assets together into one solution. It delivers world class results. My compliments to an incredible, successful effort in the midst of little to no technical guidance from Microsoft. WOW!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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