Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2013-01-25
Medium Priority
Last Modified: 2013-12-18
Hi, this is a follow-up to an answered question I had from 2009:


The code Bill helped me with has worked perfectly over the past few years, but unfortunately, my desktop got rebuilt last weekend and all my apps including Office and Notes were reinstalled.  Now, when I run that same code, I get a run-time error 91: Object variable or With block variable not set at line 71 of ComposeMemo: Call uidoc.GotoField("Body")

Any idea what happened and how can I fix it?

Thank you.

Below is the code:

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:\temp\any_image.gif"
  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_IDENTITY_8BIT = 1729
  ' 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
  ' 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
  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
Question by:Hammer8
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3

Expert Comment

ID: 38819921
Try explicitly declaring your objects and use the NEW keyword  


Dim sess As Object, db As Object .... etc


Dim sess As New Domino.NotesSession    
Dim db As New Domino.NotesDatabase

Author Comment

ID: 38820051
Hi, I tried that, but now get an error message 13 Type Mismatch at:

Set sess = CreateObject("Notes.NotesSession")

Expert Comment

ID: 38820086
Set sess = CreateObject("Domino.NotesSession")
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 38820094
I assume you made a reference the the correct library..  Just press F2 to se a listing in the Object Browser...

Author Comment

ID: 38820227
Yes, I have references to:

Lotus Domino Objects
Lotus Notes Automation Classes

Now, I get an error 429:  ActiveX component can't create object.
LVL 46

Expert Comment

by:Sjef Bosman
ID: 38821749
The original code seems ok. It could have something to do with rights in the database, or with the fact that the doc is removed wile the uidoc is still in use.

In order to get a quick resulution of your problem, I'll ask Bill in. That is, if i still have his mail address somewhere...

Author Comment

ID: 38821922
That would be much appreciated...thank you.
LVL 46

Accepted Solution

Sjef Bosman earned 2000 total points
ID: 38823695
Mail sent.

Expert Comment

ID: 38826479
I suggest you try a test project just to see if all the required objects load and that you can make a connection to the database object.

Author Comment

ID: 38827680
Hi, I had my IT person reinstall Lotus Notes and now, I am getting a different error message.  The first is Lotus Notes itself now gives me a pop up which says Cannot locate Form: Memo.  And after I hit "OK" to that, in VBA, I get error message:  Run-time error '7412':  Notes Error - Cannot locate Default Form.  This occurs at line "Set uidoc = ws.EDITDOCUMENT(True, doc)"

Maybe this helps to isolate the problem?

Thanks you all again!

Expert Comment

ID: 38827870
same problem...   Dim ws As Object   won't work..

Try Dim ws As New As NotesUIWorkspace

See this link;


Author Comment

ID: 38833473
Thank you all for your help.  I'm happy to report that the problem was solved after we "sync'ed/replicated" something called Application templates (NTF)'s.  I'm not sure what those are, but now that it's sync'ed with the server, the macro works as before.  Hopefully someone else with the same problem will be able to find this solution if they are experiencing the same issue.  Again, your help is much appreciated!
LVL 46

Expert Comment

by:Sjef Bosman
ID: 38838774
That's more or less what I expected: the code was okay, so there was a problem elsewhere (my, really? ;-)). I'd never have guessed that replicating the NTFs would solve it. An application template is a design copy of an application, it's usually the design base of one or more identical applications. The developer uses the template to apply modifications. The template is then used to refresh all databases that are derived from the template. E.g. there is a mail template mail85.ntf with the internal name StdR85Mail; when a new release of Domino is installed and the template is modified, the template is automatically applied to all mail databases.

However, how this solved your problem I fail to see... But I'm glad that it's solved!

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question