Option Compare Database
Option Explicit
Private Const gstrObject = "basE-Mail Module"
Private strError_Message As String
Public Function fnSendNotesMail( _
Recipient As Variant, Subject As String, _
bodytext As String, SaveIt As Boolean, _
Optional Attachment As String = "", _
Optional CCRecipient As Variant = "", _
Optional bccRecipient As Variant = "")
'//////////////////////////////////////////////////////////////////
'// Function: fnSendNotesMail
'//////////////////////////////////////////////////////////////////
'// Author: John Hawkins
'//////////////////////////////////////////////////////////////////
'// Revision History:
'// 17 Jul 2013 tranchemontaigne -Added documentation
'// -Added error logging
'//
'//////////////////////////////////////////////////////////////////
'// Description:
'// This public sub will send a mail and attachment if
'// neccessary to the recipient including the body text.
'//
'//////////////////////////////////////////////////////////////////
'// Requirements:
'// Requires that notes client is installed on the system.
'//
'//////////////////////////////////////////////////////////////////
'// Inputs:
'// Argument Type Content
'// -----------------------------------------------------------
'// Subject string e-mail message subject
'// attachment string e-mail message attachment
'// recipient string message recipient
'// bodytext string body of e-mail message
'// saveit bool asd
'//
'//////////////////////////////////////////////////////////////////
'// Legal Disclaimer:
'// This code is based upon code and application content
'// downloaded from its copyright owner, of Smiley I.T. and as
'// such reproduction in any form which is for commercial use
'// requires the permission of the Webmaster. Any use of this
'// code for non-commercial use only requires a link or comment
'// back to the original page you took the code from.
'//
'// Source:
'// [URL]http://www.fabalou.com/vbandvba/lotusnotesmail.asp[/URL]
'//
'// CT's Note:
'// Commercial use has been defined in at least one instance as
'// "Any wholesale, retail,or service business activity
'// established to carry on trade for profit."
'// Source: [URL="http://www.co.loudoun.va.us/compplan/glossary.htm"]www.co.loudoun.va.us/compplan/glossary.htm[/URL]
'//
'//////////////////////////////////////////////////////////////////
'// CC or BCC message:
'// Send a message to more than one person or copy or blind
'// carbon copy
'//
'// Sample Code:
'// MailDoc.sendto = Recipient
'// MailDoc.CopyTo = ccRecipient
'// MailDoc.BlindCopyTo = bccRecipient
'//
'//////////////////////////////////////////////////////////////////
'// Multiple e-mail addresses:
'// For multiple email addresses set MailDoc.sendto to an
'// array of variants each of which will receive the message.
'//
'// Sample Code:
'// Dim recip(25) As Variant
'// recip(0) = "emailaddress1"
'// recip(1) = "emailaddress2" e.t.c
'//
'// MailDoc.sendto = recip
'//
'//////////////////////////////////////////////////////////////////
On Error GoTo Err_fnSendNotesMail
'Set up the objects required for Automation into lotus notes
Dim Maildb As Object 'The mail database
Dim UserName As String 'The current users notes name
Dim MailDbName As String 'The current users notes mail database name
Dim MailDoc As Object 'The mail document itself
Dim AttachME As Object 'The attachment richtextfile object
Dim Session As Object 'The notes session
Dim EmbedObj As Object 'The embedded object (Attachment)
'Start a session to notes
Set Session = CreateObject("Notes.NotesSession")
'Next line only works with 5.x and above. Replace password with your password
'Session.Initialize ("password")
'Get the sessions username and then calculate the mail file name
'You may or may not need this as for MailDBname with some systems you
'can pass an empty string or using above password you can use other mailboxes.
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)
If Maildb.ISOPEN = True Then
'Already open for mail
Else
Maildb.OPENMAIL
End If
'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
MailDoc.sendto = Recipient
If CCRecipient(1) <> "" Then
MailDoc.CopyTo = CCRecipient
End If
If bccRecipient(1) <> "" Then
MailDoc.BlindCopyTo = bccRecipient
End If
MailDoc.Subject = Subject
MailDoc.Body = bodytext
MailDoc.SAVEMESSAGEONSEND = SaveIt
'Set up the embedded object and attachment and attach it
If Attachment <> "" Then
Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
'MailDoc.CREATERICHTEXTITEM ("Attachment")
End If
'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
MailDoc.SEND 0, Recipient
Exit_fnSendNotesMail:
'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
Exit Function
Err_fnSendNotesMail:
strError_Message = "Error: " & Chr(10) & Chr(13) & _
" " & Err.Number & ": " & Err.Description
MsgBox strError_Message, vbCritical, "Error: cmdNetworkPath_Click"
Call fnLogError(gstrObject, "cmdNetworkPath_Click", strError_Message)
Resume Exit_fnSendNotesMail
End Function