Link to home
Start Free TrialLog in
Avatar of waverazor
waverazor

asked on

Lotus Notes Automation using Visual Basic

Using Visual Basic 2008 and Lotus Notes 8.5.

I use this code to automate the Lotus Notes UI to create an HTML format email with attachment.
sess = CreateObject("Notes.NotesSession")
ws = CreateObject("Notes.NotesUiWorkspace")
db = sess.GetDatabase("", "", False)

If Not db.IsOpen Then db.OpenMail()
doc = db.CreateDocument()
Call doc.ReplaceItemValue("Form", "Memo")

' add the body as a mime html part
convertMime = sess.convertMime
sess.convertMime = False
stream = sess.CreateStream()

stream.WriteText(strBody)
mimeBody = doc.GetMIMEEntity("Body")

If mimeBody Is Nothing Then
     mimeBody = doc.CreateMIMEEntity("Body")
End If

mimeHtml = mimeBody.CreateChildEntity()
Call mimeHtml.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)
Call stream.Close()

'handle the attachment
If Attachment.Length > 0 Then
                mimeFile = mimeBody.CreateChildEntity()
                mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
                Call mimeHeader.SetHeaderVal("binary")
                mimeHeader = mimeFile.CreateHeader("Content-Disposition")
                Call mimeHeader.SetHeaderVal("attachment; filename=" & "Order Acknowledgement.pdf")
                Call stream.Open(Attachment, "binary")
                Call mimeFile.SetContentFromBytes(stream, "text/plain", ENC_NONE)
                Call mimeFile.EncodeContent(ENC_IDENTITY_8BIT)
                Call stream.Close()
End If

Open in new window


When I continue the code with this:
            Call doc.Save(True, False)
            uidoc = ws.EditDocument(True, doc)
            Call uidoc.FieldSetText("EnterSendTo", [To])
            Call uidoc.FieldSetText("EnterCopyTo", [CC])
            Call uidoc.FieldSetText("Subject", Subject)

            ' bring the window to the front
            Call AppActivate(uidoc.WindowTitle)

Open in new window

the email is created with the attachment, but the signature block is missing.  I don't beileve the default lotus notes template is being used.

When I substitute the second block of code with this:
           'copy generated HTML to clipboard
            Call doc.Save(True, False)
            uidoc = ws.EditDocument(True, doc)

            Call doc.Remove(True)
            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
            uidoc = ws.ComposeDocument(db.Server, db.filePath, "Memo")
            Call uidoc.GotoField("Body")
            Call uidoc.Paste()

            Call uidoc.FieldSetText("EnterSendTo", [To])
            Call uidoc.FieldSetText("EnterCopyTo", [CC])
            Call uidoc.FieldSetText("Subject", Subject)

Open in new window

then the signature block is there, but the attachment is not copied (missing) from the email.  It seems that the default Lotus Notes template is being used.

I need help with the code so that a HTML format email with attachment is created that uses the default template and has the signature block inserted.  I think the "CreateDocument" and "ComposeDocument" commands use two different templates.

Thanks.


ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, CreateDocument is back-end and ComposeDocument is front-end, but I use front-end and back-end classes together all the time.  You just need to be aware of the differences, how they relate, and when to use each.  The reason that CreateDocument does not set the signature is because there are no hooks that allow the developer to extend the functionality.  When ComposeDocument is used, however, code in the Form's events are executed.  This is how you extend functionality of the ComposeDocument and EditDocument methods.  The Memo form probably uses the QueryOpen or PostOpen event to check whether the document is new - if it is, the signature is added.

The best solution is to create the document in the backend adding in the signature as specified by the CalendarProfile document (you could look at the Lotus code to see how they are doing this), then display it to the user using ws.EditDocument.

That's a lot of work, though.  As a workaround, I have done this in the past...
1) Create back-end document using mime or richtext.
2) Display it temporarily using ws.EditDocument. (it is open for only an instant.  most users won't notice)
3) Copy the body to the clipboard using uidoc.Copy.
4) Close the temp uidoc.
5) Compose new memo.
6) Paste in the body using uidoc.Paste.

Not the best solution because it destroys the contents of the clipboard, but it is the only way I have found to compose a mime or richtext memo with a signature.
>  You just need to be aware of the differences...
With an emphasis on "just"... ;-) I mentioned "better not", for the reasons you mentioned and also the fact that rich-text fields aren't synchronised.

I almost agree with the last sentence, for it should be possible to include the Signature from the profile document using only the back-end class. Also a lot of work...
Avatar of waverazor
waverazor

ASKER

Sjef,

Using the code below got me the signature, but the new line characters where not recognized in the html email.  My signature appeared like this (on one line): Donald Duck Any Company 123 Main St (123) 456-7890.
 
Dim profdoc As Notesdocument
Set profdoc = maildb.GetProfileDocument("CalendarProfile").("Signature")(0)

I added this like of code and the signature is formated with line breaks:

stream.WriteText(sig.ToString.Replace(vbCrLf, "<br>"))

Bill,

You've help me very much in the past.  The code I use is yours.  I believe that I have already tried what you suggest in your workaround (the 3rd block of code in my 1st posting), but the attachment does not get copied to the new email.  When I use the 2nd block of code in the 1st posting (I get the attachment, but I lose the signature) the attachment is not in the body of the email, but in the heading portion to the left of the To, cc, bcc, and subject lines.  Maybe this location is why it is not copied to the new email?  If my 3rd block of code is not what you suggest, please provide a code sample.

Thanks.
I just tested, and you're right.  My workaround no longer works because 8.5 changes the way that MIME attachments are displayed.  Since they are no longer rendered in the body field, the copy-paste solution no longer works.

The only true solution I know of is to code the 'insert signature' part by hand.  I would start by looking at the UIMemoDocument.InsertSignature method in the mail file design.  It should have everything you need.
The solution should include: profdoc.GetItemValue("Signature")(0) to get the text signature.

However, this will not work for a HTML signature.  For a HTML signature I read the HTML file and appended the contents to the body.