Search code examples
vbadatabaseemaillotus

How will affect Lotus Notes GetDatabase parameters my notes account?


I would like to send e-mail with excel macro. I have read on some websites the same simple VBA code which can send the e-mail with attachment.

Sub Send_Email_via_Lotus_Notes()
    Dim Maildb As Object
    Dim MailDoc As Object
    Dim Body As Object
    Dim Session As Object
    'Start a session of Lotus 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 provide password of the current ID (to avoid Password prompt)
        'Call Session.Initialize("<password>")
    'Open the Mail Database of your Lotus Notes
        Set Maildb = Session.GETDATABASE("", "D:\Notes\data\Mail\eXceLiTems.nsf")
        If Not Maildb.IsOpen = True Then Call Maildb.Open
    'Create the Mail Document
        Set MailDoc = Maildb.CREATEDOCUMENT
        Call MailDoc.REPLACEITEMVALUE("Form", "Memo")
    'Set the Recipient of the mail
        Call MailDoc.REPLACEITEMVALUE("SendTo", "Ashish Jain")
    'Set subject of the mail
        Call MailDoc.REPLACEITEMVALUE("Subject", "Subject Text")
    'Create and set the Body content of the mail
        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:\dummy.txt", "Attachment")
    'Example to save the message (optional) in Sent items
        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 the Object variables - Recover memory
        Set Maildb = Nothing
        Set MailDoc = Nothing
        Set Body = Nothing
        Set Session = Nothing
End Sub

Set Maildb = Session.GETDATABASE("", "D:\Notes\data\Mail\eXceLiTems.nsf") On my working laptop there are 10 nsf file. I dont know which should I put in the second parameter. I have read the syntax here: https://help.hcltechsw.com/dom_designer/9.0.1/appdev/H_GETDATABASE_METHOD.html

Both can be empty string. If i would use empty string it creates a new database if I am correct. Because I would like to send each day 5 emails, I would like to send e-mails in for loop. If I use empty string, the code will create each day 5 database? I think yes, so I think I need one of the 10 nsf file use in as second parameter, so it will not create, but I dont want to crash my notes account with this. I am quite new to notes. I used vba for outlook to send emails, and there was no database parameters.


Solution

  • First of all: GetDatabase will NEVER create a new database. If the database you enter exists, then your OBJECT (not the real thing, just a variable) will be created and the isOpen- Property will be true, otherwise it will be false.

    You need to decide, where you want to (at least temporarily) STORE the mails you are sending.

    If you set SaveMessageOnSend = True then it will be saved in that database, if you set it to False, then it will only be created in memory and NOT saved, but still you need a container for that "in Memory"- document.

    Usually Mails that are created programmatically will be saved in the users' mailfile (in that case: in YOUR mailfile.

    The right code for this would be:

    'Initialize object without really opening a database
    Set Maildb = Session.GETDATABASE("", "")
    'Now open the users' mailfile
    Call Maildb.OpenMail
    

    If you have some "dummy" database to create your mails in, then you need to CREATE that database before running your script (within Notes\Data - Directory) using your Notes Client and open that one:

    If you e.g. create it in mail- subdirectory of Notes\Data then it might have the absolute Path like:

    C:\Program Files (x86)\HCL\Notes\Data\mail\dummy.nsf

    In your script you can address it using a relative path (starting from data):

    Set Maildb = Session.GETDATABASE("", "mail\dummy.nsf")
    

    or an absolute path (doesn't really matter) like:

    Set Maildb = Session.GETDATABASE("", "C:\Program Files (x86)\HCL\Notes\Data\mail\dummy.nsf")
    

    no need of "OpenMail"- command in that case as you do not want to use the users' mailfile but the explicitely given one...

    One more thing: Call MailDoc.SEND(False) already creates a PostedDate- item on your mail. No need to use the line Call MailDoc.REPLACEITEMVALUE("PostedDate", Now()).