Add greeting to email template by hour of day

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Add greeting to email template by hour of day

Post by gailb »

I have this working fine to open up a template. What I would like to do now is add a greeting.

How can I add the greeting based on the time of day? I've included a Case statement, just not sure how to append the strGreeting to the template.

Of course I just want to add this greeting at the top of the email.

Code: Select all

Sub CreateFromTemplate()
    
    Dim MyWeekNum: MyWeekNum = Format(Date, "ww")
    Dim strGreeting As String  Dim MyItem As Outlook.MailItem
    Set MyItem = Application.CreateItemFromTemplate("C:\Users\gb\AppData\Roaming\Microsoft\Templates\Newsletter.oft")
    
    Select Case Time
        Case Is < TimeValue("12:00"): strGreeting = "Good morning,"
        Case Is < TimeValue("16:30"): strGreeting = "Good afternoon,"
        Case Else: strGreeting = "Good evening,"
    End Select
    
    On Error Resume Next
    With MyItem
        .To = "somebody@somewhere.net"
        .Subject = "Newsletter - Vol. 21 / No. " & MyWeekNum & " (" & Format(Date, "mmm dd, yyyy") & ")"
        .Display
    End With
    On Error GoTo 0

    Set MyItem = Nothing
    
End Sub

User avatar
HansV
Administrator
Posts: 78153
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add greeting to email template by hour of day

Post by HansV »

Add the following line above .Display:

Code: Select all

        .Body = strGreeting & vbCrLf & .Body
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Add greeting to email template by hour of day

Post by gailb »

Hi Hans,

Well, I guess, unintended consequences. In my template, I have some tables. This pulls all that information out of the tables and displays it as string text.

User avatar
HansV
Administrator
Posts: 78153
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add greeting to email template by hour of day

Post by HansV »

How about

Code: Select all

        .HTMLBody = strGreeting & "<p>" & .HTMLBody
If that doesn't work, we'll have to use a trick.
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Add greeting to email template by hour of day

Post by gailb »

That did it perfect. Thanks.