Link to home
Start Free TrialLog in
Avatar of Dian S
Dian S

asked on

Inserting .html or .jpg as signature in lotusnotes via access vba

Hi, I would like to send email via lotus notes using access vba. It works well but I need help to insert a html or .jpg  signature in the mail. Could you please help me with access vba code?

Thanks
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Usually the "easiest" way to send a mail in Notes with special formatting is by using MIME. This page gives you an example:

https://www.experts-exchange.com/questions/28008749/VBA-and-Lotus-Notes-Using-VBA-to-create-Notes-email-with-2-or-more-attachments-and-have-Notes-open-email-for-user-edit-before-sending.html

The code has to be tinkered a little if you don't want to show the mail to the user before sending it away.
Avatar of Dian S
Dian S

ASKER

Hi Sjef,

Many thanks for your help. I can run the code without error but the signature attachment that I would like to insert in mail does not show up.
I really have no idea which part that I should amended to refer to image/html file  that I would like to insert .
Would you kindly please help? Do the image should be always in .gif? Is it also possible in .jpg?
Many thanks,

Sub Test_GIF()
 
  Dim html As String
  Dim attachments(0) As String, imageFiles(0) As String, imageTypes(0) As String, imageTags(0) As String
  html = "<div style='font-size: 10pt; font-family: Arial, Helvetica, sans-serif; font-weight: bold;'><img src='cid:image_1.gif'>This is a test!</div>"
  attachments(0) = "c:\temp\any_file.txt"
  imageFiles(0) = "C:\LogFiles\UserTemp\Access\Signature.jpg"
  imageTypes(0) = "image/gif"
  imageTags(0) = "image_1.gif"
  Call ComposeMemo("some.user@acme.com", "Test from Excel", html, attachments, imageFiles, imageTypes, imageTags)
 
End Sub



Sub ComposeMemo(sendto As String, ByVal subject As String, ByVal html As String, attachments() As String, imageFiles() As String, imageTypes() As String, imageTags() As String)
 
  Dim sess As Object, db As Object, doc As Object, stream As Object, ws As Object, uidoc As Object
  Dim mimeBody As Object, mimeHtml As Object, mimeFile As Object, mimeImage As Object, mimeHeader As Object
  Dim i As Integer
  Dim convertMime As Boolean
  Const ENC_QUOTED_PRINTABLE = 1726
  Const ENC_IDENTITY_8BIT = 1729
  Const EMBED_ATTACHMENT = 1454
 
  ' Create an email doc
  Set sess = CreateObject("Notes.NotesSession")
  Set ws = CreateObject("Notes.NotesUiWorkspace")
  Set db = sess.GETDATABASE("", "")
  Call db.OPENMAIL
  Set doc = db.CREATEDOCUMENT()
  Call doc.ReplaceItemValue("Form", "Memo")
 
  ' add the body as a mime html part
  convertMime = sess.convertMime
  sess.convertMime = False
  Set stream = sess.CreateStream()
  stream.WriteText (html & "<br><br>")
  Set mimeBody = doc.CreateMIMEEntity("Body")
  Set mimeHtml = mimeBody.CreateChildEntity()
  Call mimeHtml.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)
  Call stream.Close
 
  ' add file attachments
  For i = 0 To UBound(attachments)
    Set mimeFile = mimeBody.CreateChildEntity()
    Set mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
    Call mimeHeader.SetHeaderVal("binary")
    Set mimeHeader = mimeFile.CreateHeader("Content-Disposition")
    Call mimeHeader.SetHeaderVal("attachment; filename=" & attachments(i))
    Call stream.Open(attachments(i), "binary")
    Call mimeFile.SetContentFromBytes(stream, "text/plain", ENC_NONE)
    Call mimeFile.EncodeContent(ENC_IDENTITY_8BIT)
    Call stream.Close
  Next
 
  ' add images referenced by cid tags
  For i = 0 To UBound(imageFiles)
    Set mimeImage = mimeBody.CreateChildEntity()
    Set mimeHeader = mimeImage.CreateHeader("Content-ID")
    Call mimeHeader.SetHeaderVal("<" & imageTags(i) & ">")
    Call stream.Open(imageFiles(i))
    Call mimeImage.SetContentFromBytes(stream, imageTypes(i) & "; name=" + imageTags(i), ENC_IDENTITY_BINARY)
    Call stream.Close
  Next
 
  sess.convertMime = convertMime
  Call doc.CloseMIMEEntities(True, "Body")
  Call doc.Save(True, False)
  Set uidoc = ws.EditDocument(True, doc)
  Call doc.Remove(True)
  ' Exit Sub ' if you don't need the user's signature, you can exit here, otherwise...
  Call uidoc.GotoField("Body")
  Call uidoc.SelectAll
  Call uidoc.Copy
  uidoc.Document.SaveOptions = "0"
  uidoc.Document.MailOptions = "0"
  Call uidoc.Close
 
  ' compose a new memo and paste the body.
  Set uidoc = ws.ComposeDocument(db.Server, db.filePath, "Memo")
  Call uidoc.FieldSetText("EnterSendTo", sendto)
  Call uidoc.FieldSetText("Subject", subject)
  Call uidoc.GotoField("Body")
  Call uidoc.Paste
 
End Sub
Sorry, but I'm not going to do all your work. I don't mind to help out, to give hints and tips, to suggest coding changes, like the following:
- if you do not need to show the mail to the user but instead it can be sent immediately, you can remove a lot of lines:
      Set ws = CreateObject("Notes.NotesUiWorkspace")
  and all lines starting with
       Set uidoc = ws.EditDocument(True, doc)
  until
      Call uidoc.Paste
  The mail has to be sent in the end, that's where you use
      Call doc.Send(False, sendTo)

Similar code you can find here: https://www.experts-exchange.com/questions/24468076/How-can-I-code-Access-VBA-and-Lotus-Notes-to-create-an-email-with-multiple-images-and-customized-text.html
Avatar of Dian S

ASKER

Thanks for your suggestion.  I gets more tips in the link that you sent. I the image / .html is still not inserted in the mail.
What's the code that you're testing with?
Avatar of Dian S

ASKER

Sub Test_GIF()
 
  Dim html As String
  Dim attachments(0) As String, imageFiles(0) As String, imageTypes(0) As String, imageTags(0) As String
  html = "<div style='font-size: 10pt; font-family: Arial, Helvetica, sans-serif; font-weight: bold;'><img src='cid:image_1'>This is a test!</div>"
  attachments(0) = "c:\LogFiles\any_file.txt"
  imageFiles(0) = "c:\LogFiles\any_image.gif"
  imageTypes(0) = "image/gif"
  imageTags(0) = "image_1"
  Call ComposeMemo("some@acme.com", "Test from Excel", "C:\LogFiles\SignatureDian03.html", attachments, imageFiles, imageTypes, imageTags)
 
End Sub

Those codes above should refer to the location of image or .html file that I would like to insert in the mail. I have adjusted the location and the name of the file that I would like to insert in the mail. When I run the query, it runs smoothly but the image and .html file are not displayed in the mail.  I do not know where it is not correct. As per confirmation in other forum where those codes were tested in one of the member, it works fine.
The Sub is defined as
Sub ComposeMemo(sendto As String, ByVal subject As String, ByVal html As String, attachments() As String, imageFiles() As String, imageTypes() As String, imageTags() As String)

Open in new window

.

The 3rd parameter should be a string with HTML, and not a file name. If you want to get the HTML from a file, you have to open and read that file into the html variable, in order to pass it to the Sub.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.