Create email from excel with good morning/afternoon depending on time

jmcconnell

New Member
Joined
Feb 2, 2019
Messages
35
Hi,

Looking to have a button within a spreadsheet that creates an email using outlook. Rather than just saying "Hello", I'd like it to say good morning up until 12:00, good afternoon until 17:00 and good evening. I'd also like to insert a signature at the bottom. Here's what I have so far:
Rich (BB code):
Private Sub Beenageeha_Click()


    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hi," & vbNewLine & vbNewLine & _
              "Please see the fault below:" & vbNewLine & vbNewLine & _
              "Kind regards,"
                  On Error Resume Next
    With xOutMail
        .To = ""
        .CC = "insertemail@email.com; insertemail@email.com"
        .BCC = ""
        .Subject = "Beenageeha fault"
        .Body = xMailBody
        .Display
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
Any help is much appreciated.
Thanks in advance,
James.
 
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
If Time < TimeValue("12:00:00") Then
xMailBody = "Good Morning"...
ElseIf Time > TimeValue("12:00:00") And Time < TimeValue("17:00:00") Then
xMailBoxy = "Good Afternoon"...
Else 
xMailBody = "Good Evening"...
End If

Haven't tested this, so play around with it. You're basically setting your string depending on if the current times meets certain criteria.
 
Upvote 0
Thanks. That worked perfectly.

I've now added to the code so it pastes the contents of the clipboard (This is a picture from using the snipping tool) into an email. The problem i'm now having is getting the picture to display a couple of lines below the text. Just now is always appears to the right. How can I move it down a line or 2.

Thanks again for the help.
 
Upvote 0
I haven't done much coding to send out emails, so I'm not sure if this is correct, but try adding a couple of vbNewLines at the end of the string. So:

xMailBody = "your message here" & vbNewLine & vbNewLine
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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