Adding a signature to email

I am using the below code and it gives me runtime error 287 on the line signature = OutMail.HTMLBody.


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

With OutMail ' This creates a blank email and captures the users default signature.
.BodyFormat = olFormatHTML
.Display

signature = OutMail.HTMLBody


.To = " "
.CC = " "
.BCC = " "
.Subject = "Late Order Notification"
.HTMLBody = strFntNormal & strBodyText & strTableBody & "<br><br>" & signature
.Display 'or use .Send
.ReadReceiptRequested = False
End With

'outlook tidy up
Set OutMail = Nothing
Set OutApp = Nothing
 
You can't refer to Outmail.HtmlBody within the With Outmail construct.
Try this;
Code:
Dim signature as string

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail ' This creates a blank email and captures the users default signature.
	.BodyFormat = olFormatHTML
	.Display
End With

signature = OutMail.HTMLBody

With OutMail

	.To = ""
	.CC = ""
	.BCC = ""
	.Subject = "Late Order Notification"
	.HTMLBody = strFntNormal & strBodyText & strTableBody & "<br><br>" & signature
	.Display        'or use .Send

End With

'outlook tidy up
Set OutMail = Nothing
Set OutApp = Nothing

And please use code tags when posting code it makes much more forum friendly.
 
Did you add this to the top?

Code:
Dim signature as String
 
That is the code that I originally used and it just flashes the signature and opens the email with no signature.
 
I wonder if this is an outlook version issue ? That code works fine here with 2010 runtime and office 2010, 2013 and 2016 ?
 
@Minty

Hmm, good point, however, that code has been tested from Access 2003 and on so I'm thinking it's something else... a line out of place or a control reference issue.
 
So, right out of the gate...

You did not put *Option Explicit* at the top so you have undeclared variables. Well, actually you declared Outlook one way at the tip but refer to it another way in your code. The code is confused. The same with OutMail.

You did not declare *signature*

Oh, and this needs to be a Function not a Sub.

Then... what is up with all those References? Are you using them? (By the way I unchecked the one to Redemption, I don't have it (never use it).

Once I fixed all the above it works as expected. eMail opens with table and signature.
 

Attachments

Thanks for the help. I have changed the code so much that is why it is probably a mess. I tried to run it again and the signature still flashes. Weird that it works on your end and not on mine.
 
Works perfectly on my end so something else is going is. Is your Access version also 2010?
 
yes, the access version I have is 2010. I removed the references that I was not using. Are there any that I am missing?
 

Users who are viewing this thread

Back
Top Bottom