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
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")
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 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