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
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("", "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
  • 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")

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...
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.


Author Comment

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

Accepted Solution

Sjef Bosman earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now