Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2586
  • Last Modified:

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.
0
EtienneCilliers
Asked:
EtienneCilliers
  • 7
  • 5
1 Solution
 
qwaleteeCommented:
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
 
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?

Tks
0
 
qwaleteeCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
qwaleteeCommented:
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
 
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.





0
 
qwaleteeCommented:
Well, that IS odd!

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

0
 
qwaleteeCommented:
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
 
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?



0
 
qwaleteeCommented:
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
 
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?
0
 
qwaleteeCommented:
Not necessary.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now