Solved

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

Posted on 2003-12-09
12
2,498 Views
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.
0
Comment
Question by:EtienneCilliers
  • 7
  • 5
12 Comments
 
LVL 31

Expert Comment

by:qwaletee
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"
form.selectAll
form.copy

'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"
form.Paste

'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
form.Save
Dim doc as notesDocument
Set doc = form.document
Dim docID as String
docID = document.noteID
Dim mailFile as Object
Set mailFile = document.parentDatabase
form.Close
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,
qwaletee
0
 

Author Comment

by:EtienneCilliers
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?

Tks
0
 
LVL 31

Accepted Solution

by:
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.
0
 
LVL 31

Expert Comment

by:qwaletee
ID: 9965440
Etienne,

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.

ONE IMPORTANT OBSERVATION:
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.
0
 

Author Comment

by:EtienneCilliers
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.





0
 
LVL 31

Expert Comment

by:qwaletee
ID: 9970754
Well, that IS odd!

Perhaps we should switch to using exclusively back-end classes?  No OLE automation involved?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:EtienneCilliers
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.

0
 
LVL 31

Expert Comment

by:qwaletee
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)
Loop

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

becomes:

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




0
 

Author Comment

by:EtienneCilliers
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?



0
 
LVL 31

Expert Comment

by:qwaletee
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)
0
 

Author Comment

by:EtienneCilliers
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?
0
 
LVL 31

Expert Comment

by:qwaletee
ID: 9991904
Not necessary.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

14 Experts available now in Live!

Get 1:1 Help Now