0

I update the dates within the subject line and body.

I am trying to add my default signature to the end of the email.

Sub AutomatedMessage()

    Dim OApp As Object, OMail As Object, signature As String

    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)

    With OMail
        .Display
    End With

    signature = OMail.body
    
    start_date = Format(Now, "dddd d mmmm")
    end_date = Format(Now + 6, "dddd d mmmm")
    start_date_short = Format(Now, "d mmmm")
    end_date_short = Format(Now + 6, "d mmmm yyyy")

    With OMail
        .To = email@company.com
        .CC = email@company.com
        .Subject = "Email subject" & start_date_short & " to " & end_date_short
        .Body = "Dear people, etc etc starting" & start_date & " and ending" & end_date & signature
    End With

    Set OMail = Nothing
    Set OApp = Nothing

End Sub

This results in a runtime error 287.

When debugging, the .body after .Display has only contains <>, even though I can see the signature is in the email when it has opened.

Community
  • 1
  • 1
Axil
  • 1
  • 3
    Does this answer your question? [How to add default signature in Outlook](https://stackoverflow.com/questions/8994116/how-to-add-default-signature-in-outlook) – braX Oct 01 '20 at 02:21
  • If there are pictures then `.HTMLBody` instead of `.Body`. – niton Oct 01 '20 at 02:45
  • The linked article is the one that I originally wrote this based off of. Even using HTMLBody instead of body though, every time it gets to saving the HTMLBody it throws Run-time error '287'. Opening the debugger shows that the `.HTMLBody` and `.Body` are empty even though the displayed email has content in it. Most things I've seen use the same method, and give me the same error. – Axil Oct 08 '20 at 04:29

1 Answers1

1

Enclose your email addresses with quotation marks as they are expected to be string inputs.

.To = "email@company.com"
.CC = "email@company.com"

Also add some white spaces to separate the different values in your text. So the full method (tested and working) would look like the below:

Sub AutomatedMessage()

    Dim OApp As Object, OMail As Object, signature As String

    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)

    With OMail
        .Display
    End With

    signature = OMail.Body
    
    start_date = Format(Now, "dddd d mmmm")
    end_date = Format(Now + 6, "dddd d mmmm")
    start_date_short = Format(Now, "d mmmm")
    end_date_short = Format(Now + 6, "d mmmm yyyy")

    With OMail
        .To = "email@company.com"
        .CC = "email@company.com"
        .Subject = "Email subject " & start_date_short & " to " & end_date_short
        .Body = "Dear people, etc etc starting " & start_date & " and ending " & end_date & signature
    End With

    Set OMail = Nothing
    Set OApp = Nothing

End Sub
mtholen
  • 1,631
  • 2
  • 15
  • 27
  • It will loose any formatting of signature like color, style, bold italic etc. It will add signature as plain text. – Harun24hr Nov 06 '20 at 03:24