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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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,
EtienneCilliersAuthor Commented:
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?

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.


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.
EtienneCilliersAuthor Commented:
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.

Well, that IS odd!

Perhaps we should switch to using exclusively back-end classes?  No OLE automation involved?
EtienneCilliersAuthor Commented:
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.

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

EtienneCilliersAuthor Commented:
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?

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)
EtienneCilliersAuthor Commented:
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?
Not necessary.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.