Adding a signature to email

rywello

Registered User.
Local time
Today, 16:51
Joined
Jan 13, 2016
Messages
68
I am trying to add a signature to an outgoing outlook message through access. I am using the below code which only adds the signature to the email and does not include the body of the email. I also get the run-time error 287: Application-defined or object-defined error. This application will be used by multiple users so I want their signatures to auto-fill.

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

With OMail
.Display
End With
signature = OMail.Body

With OMail
'Set body format to HTML
.To = " "
.Subject = "Late Order Notifications"
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>" & strFntNormal & BodyText & strTableBody & " </BODY></HTML>" & vbNewLine & signature
.Display
End With

Any help will be appreciated.
 
Try...

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

     With OMail
     'Set body format to HTML
       .To = ""
       .Display
        .Subject = "Late Order Notifications"
       .HTMLBody = strFntNormal & BodyText & strTableBody & "<br>" & .HTMLBody
      End With
 
I am still getting error 287: Application-defined or object-defined error and the it does not show the body of the email.
 
It highlights the code: .HTMLBody = strFntNormal & BodyText & strTableBody & "<br>" & .HTMLBody
 
You create Omail as a new (empty) instance of a mail object
Set OMail = OApp.CreateItem(0)
Then set signature to an empty sting
signature = OMail.Body
Then append it to a HTML string. You will obviously get nothing.

You need to have signature equal to the full path to the signature image file you want to incorporate in the email and then encaptulate that in picture tags in your HTML.
"<IMG SRC='your file path.jpg'/>"
 
strFntNormal = "<font color=black face=" & Chr(34) & "Arial" & Chr(34) & " size=2>"
BodyText = Text want in the body
strTableBody= the table that I have in the email
 
Umm, okay, what defines that in your code? Where is the BodyText and the Table? It's not declared in the section of code you posted.
 
Sub ReportOutlookBody()



Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim strBodyText As String
Dim OlApp As Outlook.Application
Dim ObjMail As Outlook.MailItem

Dim strTableBeg As String
Dim strTableBody As String
Dim strTableEnd As String
Dim strFntNormal As String
Dim strTableHeader As String
Dim strFntEnd As String
Dim lngID As Long
Dim strSQL As String
Dim mailaddress As Variant
Dim strSubject As String
Dim OApp As Object, OMail As Object, signature As String

'Define format for output
strTableBeg = "<br><br><table border=1 cellpadding=3 cellspacing=0>"
strTableEnd = "</table><BR><BR>Please be assured that we are making best efforts to optimize our supply resources, which should minimize any further delays."
strTableHeader = "<font size=3 face=" & Chr(34) & "Arial" & Chr(34) & "><b>" & _
"<tr bgcolor=lightblue>" & _
TD("Customer:") & _
TD("Ship-to:") & _
TD("Order Number:") & _
TD("Purchase Order:") & _
TD("Customer:") & _
TD("Product:") & _
TD("Requested Ship Date:") & _
TD("Plant Anticipated Date:") & _
"</tr></b></font>"
StrContent = StrContent & "Limit Changes: " & Forms!Main.lstorders.Column(1) & Chr(13)
strFntNnormal = "<font color=black face=" & Chr(34) & "Arial" & Chr(34) & " size=2>"
strFntEnd = "</font>"



Dim strList As String
Dim varItem As Variant
Dim i As Long
Set lst = Forms!Main!lstorders
If lst.MultiSelect Then
With lst
For i = 1 To lst.ListCount - 1
.Selected(i) = True
Next i
End With
End If
On Error GoTo 0

' now call the after update event of the listbox merely to check the cmdsend button caption
' Call lstorders_AfterUpdate

i = 0


With Forms!Main!lstorders
For Each varItem In Forms!Main!lstorders.ItemsSelected
strList = strList & Forms!Main!lstorders.Column(0, varItem) & ","
Next
If strList <> "" Then
strList = Left(strList, Len(strList) - 1)
strList = "(" & strList & ")"
strSQL = "SELECT * FROM tblTrucks WHERE [ID] IN " & strList & ";"
'(send e-mail)
Else
MsgBox ("Please select an order from the list.")

End If

End With

Set rst = CurrentDb.OpenRecordset(strSQL)
strTableBody = strTableBeg & strFntNormal & strTableHeader

Do Until rst.EOF
strTableBody = strTableBody & _
"<tr>" & _
TD(rst![Customer]) & _
TD(rst![Ship-to]) & _
TD(rst![Sales Doc]) & _
TD(rst![PO#]) & _
TD(rst![City]) & _
TD(rst![Mat Description]) & _
TD(rst![PlGI date]) & _
TD(rst![Plant Anticipated Date]) & _
"<tr>"

.BodyText = "<HTML><BODY>Hi Team,<BR><BR>Below are the Late Notifications for today. Please send out an email to the customer through the Late Order Notification Database <BR></BODY></HTML>"
rst.MoveNext
Loop

strTableBody = strTableBody & strFntEnd & strTableEnd

rst.Close

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

With OMail
.Display
End With
signature = OMail.Body

With OMail
'Set body format to HTML
.To = " "
.Subject = "Late Order Notifications"
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>" & strFntNormal & BodyText & strTableBody & " </BODY></HTML>" & vbNewLine & signature
.Display
End With

Set OMail = Nothing
Set OApp = Nothing

Clean_Up:

Set rst = Nothing


End Sub
 
Okay, let's first put a period before BodyText (.BodyText) in my section of code.

That said I would have used two Functions, one to create the Body of the eMail and the other to send the eMail calling the first part in the second part.
 
@Cronk...

Not a good idea if you are going to distribute Frontends.
 
If you put a path to the signature file...the name of the file will change on different users computers. Is there a way to have it work to pull the default signature, no matter who is using the application?
 
No, because each person can store the signature in a different place, hence doing it via code because Outlook keeps it in one place. The way I have posted is the way I am using it... if it is still giving you problems then I would suggest breaking it up into two separate functions.
 
I split the code into 2 functions as below. I am not sure how to call the first part in the second part. Oh, and BodyText is actually a string. Thanks for your help.

Function ReportOutlookBody()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim BodyText As String
Dim OlApp As Outlook.Application
Dim ObjMail As Outlook.MailItem

Dim strTableBeg As String
Dim strTableBody As String
Dim strTableEnd As String
Dim strFntNormal As String
Dim strTableHeader As String
Dim strFntEnd As String
Dim lngID As Long
Dim strSQL As String
Dim mailaddress As Variant
Dim strSubject As String
Dim OApp As Object, OMail As Object, signature As String

'Define format for output
strTableBeg = "<br><br><table border=1 cellpadding=3 cellspacing=0>"
strTableEnd = "</table><BR><BR>Please be assured that we are making best efforts to optimize our supply resources, which should minimize any further delays."
strTableHeader = "<font size=3 face=" & Chr(34) & "Arial" & Chr(34) & "><b>" & _
"<tr bgcolor=lightblue>" & _
TD("Customer:") & _
TD("Ship-to:") & _
TD("Order Number:") & _
TD("Purchase Order:") & _
TD("Customer:") & _
TD("Product:") & _
TD("Requested Ship Date:") & _
TD("Plant Anticipated Date:") & _
"</tr></b></font>"
StrContent = StrContent & "Limit Changes: " & Forms!Main.lstorders.Column(1) & Chr(13)
strFntNnormal = "<font color=black face=" & Chr(34) & "Arial" & Chr(34) & " size=2>"
strFntEnd = "</font>"



Dim strList As String
Dim varItem As Variant
Dim i As Long
Set lst = Forms!Main!lstorders
If lst.MultiSelect Then
With lst
For i = 1 To lst.ListCount - 1
.Selected(i) = True
Next i
End With
End If
On Error GoTo 0

' now call the after update event of the listbox merely to check the cmdsend button caption
' Call lstorders_AfterUpdate

i = 0


With Forms!Main!lstorders
For Each varItem In Forms!Main!lstorders.ItemsSelected
strList = strList & Forms!Main!lstorders.Column(0, varItem) & ","
Next
If strList <> "" Then
strList = Left(strList, Len(strList) - 1)
strList = "(" & strList & ")"
strSQL = "SELECT * FROM tblTrucks WHERE [ID] IN " & strList & ";"
'(send e-mail)
Else
MsgBox ("Please select an order from the list.")

End If

End With

Set rst = CurrentDb.OpenRecordset(strSQL)
strTableBody = strTableBeg & strFntNormal & strTableHeader

Do Until rst.EOF
strTableBody = strTableBody & _
"<tr>" & _
TD(rst![Customer]) & _
TD(rst![Ship-to]) & _
TD(rst![Sales Doc]) & _
TD(rst![PO#]) & _
TD(rst![City]) & _
TD(rst![Mat Description]) & _
TD(rst![PlGI date]) & _
TD(rst![Plant Anticipated Date]) & _
"<tr>"

BodyText = "<HTML><BODY>Hi Team,<BR><BR>Below are the Late Notifications for today. Please send out an email to the customer through the Late Order Notification Database <BR></BODY></HTML>"
rst.MoveNext
Loop

strTableBody = strTableBody & strFntEnd & strTableEnd

rst.Close
Set rst = Nothing
End Function

Function SendeMail()

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

With OMail
'Set body format to HTML
.To = ""
.Display
.Subject = "Late Order Notifications"
.HTMLBody = strFntNormal & BodyText & strTableBody & "<br>" & .HTMLBody
End With

Set OMail = Nothing
Set OApp = Nothing

End Function
 
No, I got the same error. Am I missing something in the code?
 
Not sure, is what you last posted what you are using? If yes, BodyText still missing the period.
 
The BodyText is working fine. When I put a period I get the error that it is unqualified. That part of the code worked before I stared trying to code the signature. For some reason I can not get the signature code to work.
 
This definitely works and captures the users default signature ;
Code:
   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 = Variable_To
        .CC = ""
        .BCC = ""
        .Subject = Variable_Subject
        .HTMLBody = Variable_Body & signature
        .Display        'or use .Send
        .ReadReceiptRequested = False
    End With

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

I don't think you can set .HtmlBody to itself with strBodyTxt & .HtmlBody
 

Users who are viewing this thread

Back
Top Bottom