-2

I got the following from How to add default signature in Outlook. However, I keep getting an error "Application-defined or object-defined error" where the signature variable is set. How can I fix this error?

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
With OMail
    '.To = "someone@somedomain.com"
    '.Subject = "Type your email subject here"
    '.Attachments.Add
    .body = "Add body text here" & vbNewLine & signature
    '.Send
End With
Set OMail = Nothing
Set OApp = Nothing

I know I could get the signature from the C:\Users...\Signatures\ folder. However at work, we all use virtual desktops and the permissions there are a tad janky.

Secespitus
  • 710
  • 2
  • 14
  • 22
TideRunner
  • 9
  • 1
  • 5
  • Are you using `.Body` or `.HTMLBody`? How complex is your signature - is there an embedded picture, for example? – BigBen Jul 31 '18 at 02:27
  • The signature is just simple text, no pictures or anything crazy. I could go with either, but i'd prefer to use `.HTMLBody`. But i literally copied and pasted the above code, and still get the error. Im just trying to find a starting point and cant even get there. – TideRunner Jul 31 '18 at 02:30
  • And if you step through with F8 you can see the signature once the email is displayed? – BigBen Jul 31 '18 at 02:32
  • i cant even step in it throws the error before i can even try. if i just avoid using the signature variable, it is there. – TideRunner Jul 31 '18 at 02:35

1 Answers1

1

Your signature has to be declared as a variant.

Dim OApp As Object
Dim OMail As Object
Dim Signature As Variant

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

On Error Resume Next
With OMail
    'Capture signature block.
    .Display
    Signature = .HTMLBody
    '.To = Recipients
    '.CC = CarbonCopy
    .Subject = "Subject"
    .HTMLBody = "<p>Add Body Here.</p>" & Signature
    .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
pondersome
  • 183
  • 1
  • 9