Manipulate and send a copy of current Lotus Notes email using VBA

Posted on 2003-12-09
Last Modified: 2013-12-18
I am using Lotus Notes from VBA to send out personalised reports to recipients. I have the application working in Outlook (from VBA - Excel) but now need to port this to Lotus.

The way it works is that the user sets up a generic "template" email (with attachments, formatting, etc) for the report in Notes as a normal email edit. The user leaves this email to be the "current" item, then accesses the VBA program (in Excel). The VBA program must now pick up the "current" email, copy it, add the specific report for that participant, address it and send.

I have found code to help with the basics (addressing/attaching), but I need help to
1) Find the "current" email that is open in Notes.
2) Get reference to a copy of it to be able to do the report attaching and sending.

Any help/suggestions/code appreciated. Incidentally, for Outlook, I use <<objOutlook>>.Activeinspector.CurrentItem to get hold of the current item.
Question by:EtienneCilliers
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
  • 7
  • 5
LVL 31

Expert Comment

ID: 9904158
Hi EtienneCilliers,

Dim workspace as Object
Set workspace = CreateObject("Notes.NotesUiWorkspace")
Dim form as Object 'notesUiDocument
Set form = workspace.currentDocument

Dim subject as String
subject = form.fieldGetText("Subject")

form.editMode = true 'property side-effect forces edit mode
form.gotoField "Body"

'Now, we have the document content in the clipboard, and we know the subject
Set form = workSpace.composeDocument (,,"Memo") 'now we are in a new memo
form.FieldSetText "SendTo" , "some recipient"
form.FieldSetText "Subject" , Subject
form.gotoField "Body"

'Now, we have an issue.  We need to attach the file, but the object model does not provide a "UI" method for attaching the file
'So, we save the document, retain its ID, close it, and use the back-end object model to add the attachment and send
Dim doc as notesDocument
Set doc = form.document
Dim docID as String
docID = document.noteID
Dim mailFile as Object
Set mailFile = document.parentDatabase
Set doc = mailFile.getDocumentByID(docID)
Dim body as Object
Set body = doc.getFirstItem("Body")
body.EmbedObject 1454 , "" , "c:\somefile.doc"
doc.Send False 'False prevents use of aan esoteric feature of Notes
doc.Save True , False

Best regards,

Author Comment

ID: 9904469
Thanks very much.

I haven't been able to try it, but I stepped thru and it seems solid (I would have preferred a more elegant solution, but hey).

Some questions :
- how do you check for attachments in the template and copy those accross?
- will the .copy retain any formatting in the "template" email body?

LVL 31

Accepted Solution

qwaletee earned 200 total points
ID: 9909357
Copy/paste should maintain full fidelity.  Attachments will in most cases get automatically carried over by the Copy/Paste.  Notes natively keeps attachment references in the Body field as file attachment icons in-line with the text.  When you copy the reference icon, Notes copies the file along with it, and on paste, does the same.  The only exception is that it IS possible to carry an attachmenton he document directly (without use of the message body), but that usually only happens with Internet-based mail or mail sent via one of Lotus's web clients.  I woudl assume that these template wudl be created by the user in the Notes client.

Notes also has a message templating feature built into its mail template, called "Stationery."  You may want to lok into that as an alternate templating mechanism.

I might have gone with a "more elegant" solution, as you say, using the front end classes just to determine which document is open (as I do with the destination message at the end, right before the EmbedObject).  However, there were two issues with it that I wanted to avoid:

1) If the open document is new, and has not been saved yet, it is impossible to work with it form the back-end
2) If the user modified the template using the front end, without saving it yet, the back-end reference will show the previous version

Copy/Paste avoids that.
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

LVL 31

Expert Comment

ID: 9965440

You don't need to re-open the question just to get more info.

> when it gets to line .copy, it throws an error "Automation Error - The server through an exception"

I believe that's "threw an exception."

I would run through -- not threw :) -- the steps manually, using the UI and keyboard/menu directly.  It could be there is something about the template specificaly that precludes copying.

1) If open document is in read mde, choose Actions->EditDocument
2) Makea note to yourself of the contents of the Subject field
3) Click inside the body area
4) Choose Edit->SelectAll
5) Choose Edit->Copy

Do you get an error?

You can also "mix and match."  Place an EXIT SUB above the form.copy, and the Notes client will be left in the state where it was ready to copy.

The OLE automation has been know to fail if the Notes clientis minimized, and occasionally, even if it is simply not inteh foreground.  You may wish to use SendKeys to send an Alt-F10 or Alt-Space,X (both maximize the window) to teh client, or use Windows API calls to force teh windo open and top.

Author Comment

ID: 9970706
yes it was "threw" - blame that on caffeine :)

Tried the completely manual approach and it worked fine.
Put in an AppActivate to bring Notes to the fore, same error.
Stopped code after .SelectAll. Did manual copy worked fine.

Thanks for the help.

LVL 31

Expert Comment

ID: 9970754
Well, that IS odd!

Perhaps we should switch to using exclusively back-end classes?  No OLE automation involved?

Author Comment

ID: 9970920
Would the approach then be to get the user to save the email in the "drafts" folder and then work it from there using the backend classes?

I think I am ok with the setting up and copying code,  but how will the code look to get hold of the emails in the DRAFTS folder to allow the code to show the titles to the user to make a selection? I have seen there is a "AllDocuments" method of the NotesDatabase object, but this is too general or I don't know how to parameterize it. I have done some searching and can't find a useful reference for this.

LVL 31

Expert Comment

ID: 9980377
Actually, what would probably be better is to use the built-in "stationery" view, and as the user to create templates there.

What you want to use, if you are usingteh drafts folder, is something like:

Dim docIDs() As String
Dim docSubjects As String

Set view = mailfile.getView("($Drafts)")
Set entries = view.allEntries
If entries.count = 0 Then
  'abort ... I leave this up to you
End If
Redim docIDs(entries.Count-1)
Redim docSubjects(entries.Count-1)
Set entry = entries.getFirstEntry
Do Until entry Is Nothing
  docIDs(i) = entry.noteID
  docSubjects(i) = entry.columnValues(5) 'based on standard R5 mail template column order
  i = i + 1
  Set entry = entries.getNextEntry(entry)

Then display the docSubjects array, and for whatever indexNumber is returned from teh subject array, use mailFile.getDocumentByID(docIDs(indexNumber)) to retrieve the document

If you want to use stationery, then you can do the same thing using .getView("Stationery") instead of "($Drafts)"

Note however that there are two kinds of stationery, Mail Stationery and Personal Stationery.  The only real difference between them is that Personal Stationery allows a field ABOVE the body and another field BELOW the body, for you to add a header and/or footer.  If you want to limit the choices to Mail Stationery, then modify the code a bit.  It is only a three-line change:

Set view = mailfile.getView("($Drafts)")
Set entries = view.allEntries


Set view = mailfile.getView("Stationery")
Set entries = view.getAllEntriesByKey("Memo Stationery")


Once you have your back end document (via mailfile.getDocumentByID), your code becomes:
Set doc = mailfile.getDocumentByID(docIDs(userSelectedIndexNumber))
Set newMessage = doc.copyToDatabase(mailFile) 'copying to my own database
newMessage.replaceItemValue "SendTo" , "some recipient"
newMessage.getFirstItem("Body").EmbedObject 1454 , "" , "c:\somefile.doc"
newMessage.Send false
newMessage.Save true,true


Author Comment

ID: 9983916
Thanks again for the comprehensive help.

BUT, new problem:

I get a VBA error 458 at line ... = entry.columnValues(5). ( 458: Variable uses an Automation type not supported in Visual Basic. )

[I assumed that 'Dim docSubjects As String' sb 'Dim docSubjects() As String' based on the usage?]

Any ideas?

LVL 31

Expert Comment

ID: 9986787
Interesting.  Notes passes an array of variants (which may be an array of arrays) for columnValues.  For some reason, VB was unable to handle it in this case.  We can bypass columnVluaes, and go directly to the underlying field:

= entry.document.getItemValue("Subject")(0)

Author Comment

ID: 9989912
It works!

q - thanks. I would like to allocate more points since apart from your persistence, you have exposed two techniques (one that works, and one that should work perhaps with some more tinkering).

If this is possible, how can I do that?
LVL 31

Expert Comment

ID: 9991904
Not necessary.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Lack of Storage capacity is a common problem that exists in every field of life. Here we are taking the case of Lotus Notes Emails, as we all know that we are totally depend on e-communication i.e. Emails. This article is fully dedicated to resolvin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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