excelvbalotus-notes

Add HTML signature in email Lotus Notes


i am currently struggling with adding my html signature into an email with vba. I have set up Lotus notes to add the signature automatically and it works when i write a new message manually but when i am using the macro i only see the path of the signature file (ex. C:\User1\Email signatures - 2023\AF.html). Here is the code i am using :

'Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string,saveit as Boolean)
'This public sub will send a mail and attachment if neccessary to the
'recipient including the body text.
'Requires that notes client is installed on the system.
Public Sub SendNotesMail(Subject As String, Attachment As String, Attachment1 As String, Recipient As String, ccRecipient1 As String, ccRecipient2 As String, ccRecipient3 As String, ccRecipient4 As String, BodyText As String, SaveIt As Boolean)
'Set up the objects required for Automation into lotus notes
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'THe current users notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    'Next line only works with 5.x and above. Replace password with your password
    'Get the sessions username and then calculate the mail file name
    'You may or may not need this as for MailDBname with some systems you
    'can pass an empty string or using above password you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    'Open the mail database in notes
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.IsOpen = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    
    signature = Maildb.GETPROFILEDOCUMENT("CalendarProfile").GETITEMVALUE("Signature")(0)
    
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Recipient
    MailDoc.CopyTo = Array(ccRecipient1, ccRecipient2, ccRecipient3, ccRecipient4)
    MailDoc.Subject = Subject
    MailDoc.body = BodyText & vbNewLine & vbNewLine & signature
    MailDoc.SAVEMESSAGEONSEND = SaveIt
     'Set up the embedded object and attachment and attach it
    If Attachment <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        MailDoc.CREATERICHTEXTITEM ("Attchment")
    End If
    Set EmbedObj = Nothing
    If Attachment1 <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment1")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment1, "Attachment1")
        MailDoc.CREATERICHTEXTITEM ("Attchment1")
    End If
    
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.send 0, Recipient
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
    
MsgBox "Email inviata con successo."

End Sub

Any help would be appreciated.


Solution

  • First of all: PLEASE use the right code to open the mailfile of the user. This is mostly nonsense:

    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    'Open the mail database in notes
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.IsOpen = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    

    Just replace it with

    Set Maildb = Session.GETDATABASE("", "")
    Maildb.OPENMAIL
    

    Second: Attachments in Mails go to the "Body" richtext-item not to one called "Attachment" or "Attachment1"...

    Now to your problem: In the item "Signature" in the calendar profile there is only the path to the html file. Your code simply adds that path to the body field. But you need to import the html file to the body. Unfortunately importing is only possible in a frontend UI element called NotesUIDocument and NOT in the backend as you do it at the moment.

    To fix that you need to:

    This looks like this:

    Dim body As Object 'The body of the mail
    MailDoc.Subject = Subject
    Set Body = MailDoc.CREATERICHTEXTITEM("Body")
    Body.AppendText( BodyText )
    Body.AddNewline(2)
    MailDoc.SAVEMESSAGEONSEND = SaveIt
     'Set up the embedded object and attachment and attach it
    If Attachment <> "" Then
        Body.EMBEDOBJECT(1454, "", Attachment, "Attachment")
    End If
    If Attachment1 <> "" Then
        Body.EMBEDOBJECT(1454, "", Attachment1, "Attachment1")
    End If
    
    'Create the UI Element
    Dim Workspace As Object
    Dim uidoc as Object
    Set Workspace = CreateObject("Notes.NotesUIWorkspace")
    'Send the document
    Set uidoc = Workspace.EditDocument( True, MailDoc )
    uidoc.GotoField( "Body" )
    uidoc.Import("HTML File", signature)
    uidoc.Send()
    uidoc.Save()
    uidoc.document.SaveOptions = "0"
    uidoc.Close(True)
    

    Code is untested, might need some more tweeking and might contain typos.