BusinessObjects Board

Generate Lotus Notes Email Using VBA

Greetings,

I recently had to write a “down and dirty” vb module in a report that would export from a BO 6 report to Excel and then attach the file to a Lotus Notes Email. Finally, the program would send the spreadsheet to the end-user’s In-Box.

I notice there was a “send-mail” vba download in Bob’s Download, but it did not particularly apply to Lotus Notes. So, I thought I would share this code with others who may want to generate an email from Lotus Notes. The code does not contain any error handling, so you may want to add your own to its content.

Sub SendMailAttachment()


'***************************************************************************************
' Code Created 01/20/2004, MEMSr.
' This Module was created so the end-user can automatically save as an Excel file
' after report is refreshed.  The Excel document is then attached to Lotus Notes and
' an email is sent to their inbox for review.  After review the Excel document can be
' forward to additional recipents.  Note:  You must have Lotus Notes loaded on the 
' local machine in order to run the macro.  Also, you will need to have the Lotus Notes 
' Library Reference added to the vb editor before the code will compile.
'***************************************************************************************


MsgBox "You are about to generate an email which will contain the Excel version of your report.  " & _
       "When the process is complete look in your Lotus notes In-box.  If you want to cancel this " & _
       "operation, please click cancel on next window.", vbInformation, "IMPORTANT"
       

Dim strBOdocument As String
Dim strBOUserDocsPath As String

strBOUserDocsPath = busobj.ActiveDocument.Path & "\"

strBOdocument = Application.ActiveDocument.Name

Application.ActiveDocument.SaveAs (strBOUserDocsPath & strBOdocument & ".xls")


'  The code below is used to send Lotus Notes email attachment for the newly created Excel spreadsheet(s).
'  Note:  Lotus Notes does not have to be open at the time, but must be loaded on the local machine.

Dim domSession As New NotesSession
Dim domNotesDBMailFile As NotesDatabase
Dim domNotesDocumentMemo As NotesDocument
Dim domNotesRichText As NotesRichTextItem
Dim strAttachment As String


domSession.Initialize ("")    'Prompts user for Lotus Notes Password

Set domNotesDBMailFile = domSession.GetDatabase("", "names.nsf")
Set domNotesDocumentMemo = domNotesDBMailFile.CreateDocument


Call domNotesDocumentMemo.AppendItemValue("Form", "Memo")
Call domNotesDocumentMemo.AppendItemValue("SendTo", domSession.CommonUserName)
Call domNotesDocumentMemo.AppendItemValue("Subject", strBOdocument)

Set domNotesRichText = domNotesDocumentMemo.CreateRichTextItem("Body")

strAttachment = strBOUserDocsPath & strBOdocument & ".xls"

Call domNotesRichText.EmbedObject(EMBED_ATTACHMENT, "", strAttachment, "")


domNotesRichText.AppendText (InputBox("Enter any additional text you want to appear in your email", _
"E-mail Body"))

domNotesDocumentMemo.Send (False)


End Sub


You will also need a “ThisDocument” module which would trigger the sub. The code for that is:



Private Sub Document_AfterRefresh()

ThisDocument.ExecuteMacro ("SendMailAttachment")

End Sub

or Simply:



Private Sub Document_AfterRefresh()

SendMailAttachment

End Sub

I hope you find this information helpful. Please note that variations of this code may be needed in order to accommodate your Lotus Notes platform.


MayhewM :us: (BOB member since 2003-10-22)

Michael:

You may want to also submit this to BOB’s Downloads as there is less traffic there, and the topic won’t be as hard to find. Instructions for submitting it are in the first topic in that forum. 8) Thanks for sharing.


Dave Rathbun :us: (BOB member since 2002-06-06)

Thanks Dave, I will do that.


MayhewM :us: (BOB member since 2003-10-22)

Dave. Great code btw.

One question, when you say the lotus notes library reference must be added, how do I do that? I’m looking under components and see LotNotesUI ActiveX Control model refering to file “LotNtsUI.ocx”. The code still will not run because it doesn’t recognize the Notes Object Data Types.

I appreciate your response. Thank you.


jozeph78 (BOB member since 2003-10-22)

Dave’s a great guy :yesnod: , but I believe the credit goes to Michael on this one.

From the VB Editor, go to Tools, References, and find the Lotus Notes entry in the list.


Dwayne Hoffpauir :us: (BOB member since 2002-09-19)

Doh… Indeed. THanks Michael.

I managed to find that, now I would like to remove the prompt from password. I put my password inside the quotes but it still prompts me for one. I’ve been searching IBM site but haven’t found the exact cause yet. Is the Password prompt unavoidable?


jozeph78 (BOB member since 2003-10-22)

I can’t get past the password prompt. I’ve been trying all morning to no avail. No matter what it always prompts for password to be entered on domSession.Initialize. I’ve tried both

 Dim domSession As New Domino.NotesSession
    Dim strPassword As String    
    strPassword = InputBox("Please Enter a Password", "Enter Password")
    domSession.Initialize (strPassword)

and

 Dim domSession As New Domino.NotesSession
          domSession.Initialize ("Hard Code My Password")

When hardcoding, it does not matter if i put the correct password or not, it just prompts. If i put the wrong password in the prompt it errors out saying bad password. If I type the correct password in the prompt, it works. I’m using Notes 6.0.2CF1. I do not understand and will be posting a similar issue on the IBM site.


jozeph78 (BOB member since 2003-10-22)

Hi

Further to this I have the following scenario running from VBA:

  1. Report is refreshed and filtered many times based on a variable value and saved as a pdf file.
  2. Email each pdf file to a different person

I’ve tried to plug in the Email code below in such a way that the a portion of the email code is called after each save of the file:

Sub MailToMgr(Subj As String)  'Subj creates a Subject line based on the report name
Call domNotesDocumentMemo.AppendItemValue("Form", "Memo")
Call domNotesDocumentMemo.AppendItemValue("Subject", Subj)
strAttachment = RptName  ' the report full name and path
Call domNotesRichText.EmbedObject(EMBED_ATTACHMENT, "", strAttachment, "")
domNotesDocumentMemo.Send False, "my email address"
End Sub

My test was to see if I get 2 emails myself with different subjects and 1 file in each (tested for 2 files, it will really be running for over 360 files!)

What I get is the first email has the correct Subject line and the correct file, the 2nd email has the same subject line as the first which is wrong and has both files attached! Any ideas as to what I’m missing?

I’ve stepped through the code and the subj parameter changes as it should but does not populate the subject line. TIA

Charles


daviescm (BOB member since 2003-07-28)

Charles, try using the .ReplaceItemValue instead of the .AppendItemValue on your NotesDocument Subject field.


jozeph78 (BOB member since 2003-10-22)

Thanks for that Jozeph78.

Thats solved the Subject line issue. I’ve still the issue of both files being attached to the second email rather than just 1 file. I think it may be due to the


Set domNotesRichText = domNotesDocumentMemo.CreateRichTextItem("Body")

in the first sub. How do I replace this “Body” with a new “Body” (I’ve asked myself that q a few times personally as well!!! :wink: ) so that the file in the first email is replaced with the next file, and so on, and so on.

TIA
Charles
[/code]


daviescm (BOB member since 2003-07-28)

Solved it!!

I needed to remove the item after sending it. Here’s the code:


Sub MailToMgr(Subj As String)

Call domNotesDocumentMemo.ReplaceItemValue("Form", "Memo")
Call domNotesDocumentMemo.ReplaceItemValue("Subject", Subj)
strAttachment = RptName
Call domNotesRichText.EmbedObject(EMBED_ATTACHMENT, "", strAttachment, "")
domNotesDocumentMemo.Send False, "Email Address"
strAttachment = ""
domNotesDocumentMemo.RemoveItem ("Body")  'Added this line
End Sub

Thanks for all who looked.

Charles


daviescm (BOB member since 2003-07-28)

The other problem that I had with this code as is was the prompting for password. Because I’ll be sending this via bca I can’t enter my password in the prompt.

I resolved this by construction my sessoin object as a Lotus.NotesSession instead of Domino.NotesSession. My constructor changed into this:

    Dim domSession As NotesSession
        Set domSession = CreateObject("Lotus.NotesSession")

insead of simply

 Dim domSession As New NotesSession

The Documentation on the IBM website was quite unclear and I actually spent 1hr on the phone with them regarding the issue.

I have created a more complete version of this macro which sends as a pdf instead of .xls, error trapping, creates a log file to monitor sucessful sends, cleans up the attached file, includes Parameters for CC, importance, etc…

It was originally based on the code written by MayhewM so I suppose I should ask his or a moderators permission before posting the entire chunk.


jozeph78 (BOB member since 2003-10-22)

That would be good to see. It took me a while to work out how to send CC copies, so a more complete example would be welcomed.

Rgds
Charles


daviescm (BOB member since 2003-07-28)

I won’t post until an admin or MayhewM says it’s ok.

If you want it I will gladly share via e-mail.


jozeph78 (BOB member since 2003-10-22)

Hi Jozeph78

Via email sounds good. Can I get your emial address and I’ll send you a mail to reply to.

Rgds
Charles


daviescm (BOB member since 2003-07-28)

I have a lot of this code built into my Access Modules as well. However, I am having trouble figuring out how to change the “From” email address. That is, if I automate emails I would like to assign a different email address as the origin of the email instead of me own email. That way the receipient could respond to that “inserted” email.

Is this part of it?

Set notesdb = notessession.GETDATABASE"/Servers/", “mail\someone.nsf”)

or along with a “SendTo” can I insert a “From”?

Call notesdoc.REPLACEITEMVALUE(“Sendto”, “rgvitali@mindspring.com”)
Call notesdoc.REPLACEITEMVALUE(“From”, “someoneelse@aol.com”)

Thanks for any help.

Bob


rgvitali (BOB member since 2004-04-22)

Hi to all.

how can I call Lotus address book form in VB, and then choose recipient for mail


Momak_Kr (BOB member since 2004-04-29)

Hi,

I was trying to implement the vba code to automate the sending of an excel spreadsheet via lotus notes but seem to be running into errors

this is what I have done so far in VBA -

Option Explicit

Sub test()

Dim domSession As New NOTESSESSION
Dim domNotesDBMailFile As NOTESDATABASE
Dim domNotesDocumentMemo As NOTESDOCUMENT
Dim domNotesRichText As NOTESRICHTEXTITEM
Dim strAttachment As String

End Sub

AND HAVE ADDED THE FOLLOWING REFERENCES VIA TOOLS REFERENCES
lotus notes automation classses
lotus 1-2-3
lotus approach
lotus domino objects
lotus freelance graphics
lotus word-pro release 9

i am using visual basic for applications version 6.0.

THE ERROR I KEEP GETTING WHILE TRING TO COMPILE THE VISUAL BASIC FOR APPLICATION PACKAGE IS
COMPILE ERROR - INVALID USE OF THE NEW KEYWORD.

IM GUESSING THAT THIS HAS SOMETHING TO DO WITH THE INCORRECT REFERENCE I AM USING??

can anyone please advise and let me know what is wrong with what I have done?

Many Thanks
Alroy


alroy (BOB member since 2004-05-17)

Hi,

Small point but you mention that you are trying to automate the sending of an Excel spreadsheet but your list of references does not include Microsoft Excel. Try adding Excel in and see how that goes.


Michael Abelha :australia: (BOB member since 2002-08-16)

Hi all,

Bob Vitali on Apr 22 posed the problem of overriding the “From” user id to something else other than my own.

I have the same problem. I want the from id to correspond to a common mail box id.

When I changed the preferences user configuration to the common mail box name and sent messages from notes, the common name was used. However, the program still uses my name.

One alternative I have is to create a windows logon account with the common mail box name and have the user sending the emails to logon to that. Don’t really want to do that though.

I’d appreciate it if anyone has a solution.

Mike


mhouchen (BOB member since 2004-06-17)