Thursday, October 04, 2007

Automate Lotus Notes eMail using Visual Basic

How to send Lotus Notes mail messages with Microsoft Visual Basic

The following Visual Basic code will send a Notes e-mail message. The code includes examples of code to include an attachment and to save the sent message, which are both optional and can be removed if desired.

Dim Maildb As Object
Dim MailDoc As Object
Dim Body As Object
Dim Session As Object
'Start a session to notes
Set Session = CreateObject("Lotus.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
Call Session.Initialize
'or use below to supply password of the current ID
'Call Session.Initialize("")
'Open the mail database in notes
Set Maildb = Session.GETDATABASE("", "c:\notes\data\mail\mymail.nsf")
If Not Maildb.IsOpen = True Then
Call Maildb.Open
End If
'Create the mail document
Set MailDoc = Maildb.CREATEDOCUMENT
Call MailDoc.ReplaceItemValue("Form", "Memo")
'Set the recipient
Call MailDoc.ReplaceItemValue("SendTo", "John Doe")
'Set subject
Call MailDoc.ReplaceItemValue("Subject", "Subject Text")
'Create and set the Body content
Set Body = MailDoc.CREATERICHTEXTITEM("Body")
Call Body.APPENDTEXT("Body text here")
'Example to create an attachment (optional)
Call Body.ADDNEWLINE(2)
Call Body.EMBEDOBJECT(1454, "", "C:\filename", "Attachment")
'Example to save the message (optional)
MailDoc.SAVEMESSAGEONSEND = True
'Send the document
'Gets the mail to appear in the Sent items folder
Call MailDoc.ReplaceItemValue("PostedDate", Now())
Call MailDoc.SEND(False)
'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set Body = Nothing
Set Session = Nothing

Note: The Visual Basic programmer needs to set the Reference to use Lotus Domino objects prior to implementing this function. To enable the Lotus Notes classes to appear in the Visual Basic browser, you must execute the following within VB: Select Tools, References and select the checkbox for 'Lotus Notes Automation Classes'.

The above code is from the IBM support.
GETDATABASE given here is pointing to the sample MailDB; you need to change that to your DB.

You can do that by

UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
'Open the mail database in notes
Set Maildb = Session.GETDATABASE("", MailDbName)

To send it through Microsoft Outlook refer http://vbadud.blogspot.com/2007/04/vba-email-automation-vba-mail.html

Automate Email, VBA Email, Send Email from Excel, VBA Mail automation, Mail Automation, Lotus Notes VBA, Automate Lotus Notes, Send Mail from Lotus Notes, Link Excel with Lotus Notes,Microsoft Lotus Notes Mail Automation, Excel VBA Mail, MAPI, Send Multiple eMails

17 comments:

  1. Hi!
    I'd like to know where can I inform the code the exact path of the ID archive and the NSF archive. I can't figure it out...

    Thanks in advance!

    ReplyDelete
  2. Anonymous6:10 AM

    Its the 12th line

    Set Maildb = Session.GETDATABASE("", "c:\notes\data\mail\mymail.nsf")

    ReplyDelete
  3. Anonymous8:04 AM

    thanks a lot, it's the first one I see working.

    ReplyDelete
  4. Anonymous10:47 AM

    hi... i have a problem where on most of my client's lotus notes, my VBA code is generating html format emails.... but on one client, their lotus Notes seems to be reformatting the borders and replaced it with +-| characters for the borders and completely lost the html formatting...and on their user preferences, it is set to 'html and text' format.

    can anyone help or suggest anything?

    ReplyDelete
  5. Anonymous8:57 AM

    Can someone help with code to capture information of incoming mail using VBA.

    Thanks in advance

    ReplyDelete
  6. Anonymous5:02 AM

    Can someone point me to a code which can be used to Reply to a mail in Lotus Notes?

    ReplyDelete
  7. Anonymous7:33 AM

    Hello there,

    Thank you so much for the code. I wonder if you could be so kind as to help me out with one question? How do you code VBA so that it will send you an email when there is new information available?

    I am trying to code VBA so that once an automatic system update data on a worksheet, Excel will send me an email to notify me that these data has been updated.

    Thank you very much.

    ReplyDelete
  8. Thanx a lot for the code...Really solved my problem

    ReplyDelete
  9. Anonymous8:36 AM

    Hi, I'm struggling to find a way to automate a Lotus Notes email - but instead of creating a new memo each time, I'd like to Stationery I've created.

    Hoping someone out there can help... Thank you!! Amy

    ReplyDelete
  10. Hi,
    I have an issue in putting the data in lotus notes email through vba excel.
    I have to take the content from 3 columns of the mutlitple records in a spreadsheet and put it in the lotus notes provided one of the column satisfies the particular condition. I'm done with this to.
    But the spacing between the content of column 1 & column 2 is not even for all records.

    For example
    Row#1 Text 1 Text 2
    Row#2 Text12424 Text989
    Row#3 Text38 Text7908

    I have tried filling spaces based on column width and used vbtab method. But still the text position of column 2 is not even for all the records. Even though the length of the text in column 1 for multiple records are same, they don't end in the same position because of the different width of the characters.

    For example The text "Rakesh LNU" and the text "Saxenabeat" do not end at the same position in column 1 even though they have the same number of characters.

    Hope someone can help me out. Thank you!

    ReplyDelete
  11. Anonymous10:52 AM

    The code is using late binding as all objects are generic objects and not LOTUS DOMINO objects.

    So possibly, it can be run without setting reference to LOTUS DOMINO LIBRARY.

    Currently, I am working on building a similar code. And your thread has helped me. So thanks.

    Shrivallabha

    ReplyDelete
  12. Anonymous10:11 PM

    Can someone help me that how to add recipients in "CC" of mail in above code

    ReplyDelete
  13. I wanted to insert an image in the body content of the lotus mail notes.Help me!!

    ReplyDelete
  14. Anonymous11:36 AM

    Hi I found a problem, this works if the mail you are sending is from inside your office, but what if I want to send the email to someone outside my IP and what if I want that mail to change according to a given line

    I mean if I have a db I want that if the a2 cell has "x" value then the email can be send to the address written in the a14 cell

    Is that possible?

    ReplyDelete
  15. Anonymous1:47 AM

    Hi i want to add some unique id to all the mails in particuar folder or while replying i have to add unique ids to the subject of all replying mails . is it possible . do you guys have any code for that

    ReplyDelete
  16. Anonymous10:25 AM

    How do I amend the code "Call MailDoc.SEND" to rather open the email to allow the user to amend the body text and recipients before send the email

    ReplyDelete
  17. Linas1:20 PM

    I am getting stopped at "If Not Maildb.IsOpen = True Then"
    any ideas why?

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.