I discovered, with great excitement, this article
http://www.davison.uk.net/vb2notes.asp when researching methods for
emailing from Access via Notes. Unfortunatly, when I run this I get a
Run-time error. When I run it on an XP machine it crashes, but on an
NT box it just generates an unknown error, handled by the error
handler. I have debugged and stepped through the code and have
narrowed the issue to the point at which the NotesMailMemo class tries
to establish a Notes.Session in the .SEND method.
I have referenced the Lotus Domino Odjects tlb file as explained.
I have pasted in the code I am trying to use to call this method, and
a copy of the class file provided on the website:
'************************************************* ****************************
'
' Notes Mail Memo Class
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
'
'
' Created: 1/12/98
' by: Darren Davison (da****@davison.uk.net)
' platform: VBA in Office97, VB4,5,6 (class file), or any platform
' supporting Automation (compiled DLL)
' use: send LNotes mail with/out attachments from VB/VBA.
' Error handling is basic and needs fleshing out.
'
'
' Notes:
' Add this file to a project, then declare & use variables in
' your code as in the following eg..
'
' Dim MyMemo As New NotesMailMemo
'
' With MyMemo
' 'SendTo is required, all the others are optional.
' .SendTo = "Darren Davison"
' .cc = "SomeGroup"
' .bcc = "SomeOtherGroup"
' .Priority = "H" 'high
' .Subject = "Hello!"
' .Body = "Here's a couple of files..."
' .Attach "c:\temp\myfile.txt"
' .Attach "c:\temp\myfile2.txt"
' .Signed = True
' .Encrypted = False
' End With
'
' MyMemo.Send
'
'
'
'
' Modification History
'
' Date By Notes
' -------------------------------------------------------------------------
' 10/3/99 DD Added comments since several people
' requested a copy of this file. Please
' note - this file may be freely distributed
' ONLY if it is not in any way amended. The
' author accepts no responsibility for any
' damage arising from the use or misuse of
' this code and is not available for support.
'
' 8/4/99 DD Amended class constructor to set values
' in class variables, rather than it's own
' properties.
'
' 2/9/99 DD Amended layout of this file according to
_template
' class
'
' 21/09/99 DD Amended Priority Let to read first char of
param, and
' linked 'Importance' stamp to priority.
'
' Amended Send method to add blank lines before
attach-
' ments.
'
' Added SigFile property to append to body text.
'
' 01/03/00 DD Changed references to Domino objects now that
Domino
' supports early binding (since 5.02b). The
project
' file that contains this class will need to
reference
' the library 'Lotus Domino Objects'
(domobj.tlb)
'
' 16/08/00 DD Fixed a couple of minor bugs.
'
' 15/09/00 DD Made it work with R5 (properly). Added COM
functio-
' nality for passing name/password on a server.
'
' 01/08/01 DD Tidied upa and fixed minor potential bug in
send
' method.
'
' 04/09/02 DD Minor bugfix in Priority property.
'
'************************************************* ****************************
' ----------------------------------------------------------------------------
' Module/Class options
' ----------------------------------------------------------------------------
Option Explicit
' ----------------------------------------------------------------------------
' Constants As Type = Value
' ----------------------------------------------------------------------------
Private Const EMBED_ATTACHMENT As Integer = 1454
Private Const MAIL_BOX As String = "mail.box"
Private Const DEFAULT_FORM As String = "Memo"
Private Const VB2NOTES_ERROR_SEND As Integer = 101
Private Const CMP_SRCNAME As String = "VB2Notes"
' ----------------------------------------------------------------------------
' Class Variables As Type
' ----------------------------------------------------------------------------
Private sAttachments() As String
Private sSendTo As String
Private sCC As String
Private sBcc As String
Private sBody As String
Private sSubject As String
Private sPriority As String
Private sImportance As String
Private bEncrypted As Boolean
Private bSigned As Boolean
Private sForm As String
Private sSigFile As String
' ----------------------------------------------------------------------------
' Forward Declarations / DLL Functions
' ----------------------------------------------------------------------------
' ----------------------------------------------------------------------------
' Property Let / Get
' ----------------------------------------------------------------------------
Public Property Let SendTo(strSendTo As String)
sSendTo = strSendTo
End Property
Public Property Get SendTo() As String
SendTo = sSendTo
End Property
Public Property Let CC(strCC As String)
sCC = strCC
End Property
Public Property Get CC() As String
CC = sCC
End Property
Public Property Let Bcc(strBcc As String)
sBcc = strBcc
End Property
Public Property Get Bcc() As String
Bcc = sBcc
End Property
Public Property Let Body(strBody As String)
sBody = strBody
End Property
Public Property Get Body() As String
Body = sBody
End Property
Public Property Let Subject(strSubject As String)
sSubject = strSubject
End Property
Public Property Get Subject() As String
Subject = sSubject
End Property
Public Property Let Priority(strPriority As String)
Select Case UCase(Left(strPriority, 1))
Case "H"
sPriority = "H"
sImportance = "1"
Case "L"
sPriority = "L"
sImportance = "3"
Case Else 'including "N"
sPriority = "N"
sImportance = "2"
End Select
End Property
Public Property Get Priority() As String
Priority = sPriority
End Property
Public Property Let Form(strForm As String)
sForm = strForm
End Property
Public Property Get Form() As String
Form = sForm
End Property
Public Property Let Encrypted(boolEncrypt As Boolean)
bEncrypted = boolEncrypt
End Property
Public Property Get Encrypted() As Boolean
Encrypted = bEncrypted
End Property
Public Property Let Signed(boolSign As Boolean)
bSigned = boolSign
End Property
Public Property Get Signed() As Boolean
Signed = bSigned
End Property
'*
'read-only property returns comma-separated list of
'files that are to be attached. Corresponding
'method is the public sub Attach.
'*
Public Property Get Attachments() As String
Dim f%
For f = 0 To UBound(sAttachments)
If sAttachments(f) <> "" Then Attachments = Attachments + ","
+ sAttachments(f)
Next f
'remove first comma
If Attachments <> "" Then
Attachments = Right(Attachments, Len(Attachments) - 1)
End If
End Property
Public Property Let SigFile(strSigFile As String)
'allow not file exists
sSigFile = strSigFile
End Property
Public Property Get SigFile() As String
SigFile = sSigFile
End Property
' ----------------------------------------------------------------------------
' Class Methods
' ----------------------------------------------------------------------------
Private Sub Class_Initialize()
'set default properties
sForm = DEFAULT_FORM
sCC = ""
sBcc = ""
bEncrypted = False
bSigned = False
sPriority = "N"
sImportance = "2"
ReDim Preserve sAttachments(0)
End Sub
' ----------------------------------------------------------------------------
' Methods
' ----------------------------------------------------------------------------
'*
'Files which were listed as attachments that don't
'exist at the time this function
'is called are simply ignored - no error raised.
'*
Public Sub Send(Optional sNotesUserName, Optional sNotesPassword)
On Error GoTo errSend
Dim s As NotesSession, mailDb As NotesDatabase
Dim mailDoc As NotesDocument, rtItem As NotesRichTextItem
Dim f%, fileNo%, sLine$
Set s = New NotesSession
If IsMissing(sNotesUserName) Then
'expect that a client or server is running with a compromised
password
s.Initialize
Else
'server only - not client
If IsMissing(sNotesPassword) Then
s.InitializeUsingNotesUserName CStr(sNotesUserName)
Else
s.InitializeUsingNotesUserName CStr(sNotesUserName),
CStr(sNotesPassword)
End If
End If
Set mailDb = s.GetDatabase("", MAIL_BOX)
Set mailDoc = mailDb.CreateDocument
With mailDoc
.AppendItemValue "Form", sForm
.AppendItemValue "SendTo", sSendTo
.AppendItemValue "CopyTo", sCC
.AppendItemValue "BlindCopyTo", sBcc
.AppendItemValue "Subject", sSubject
.AppendItemValue "DeliveryPriority", sPriority
.AppendItemValue "Importance", sImportance
.AppendItemValue "EncryptOnSend", bEncrypted
If bSigned Then Call .Sign
Set rtItem = .CreateRichTextItem("Body")
rtItem.AppendText sBody
rtItem.AddNewLine 2
'attempt to write sigfile
fileNo = FreeFile
Open sSigFile For Input As #fileNo
While Not EOF(fileNo)
Line Input #fileNo, sLine
rtItem.AppendText sLine
rtItem.AddNewLine 1
DoEvents
Wend
rtItem.AddNewLine 2
resAttachments:
For f = 1 To UBound(sAttachments())
If Dir(sAttachments(f)) <> "" Then
Call rtItem.EmbedObject(EMBED_ATTACHMENT, "",
sAttachments(f))
End If
Next f
Call .Send(False)
End With
resExit:
On Error Resume Next
Close
Set mailDoc = Nothing
Set mailDb = Nothing
Set s = Nothing
Exit Sub
errSend:
Dim errNo As Long
errNo = Err.Number
Select Case errNo
Case 52 To 76 'file/path/IO errors
Resume resAttachments
Case Else
're-raise error
Err.Raise VB2NOTES_ERROR_SEND + vbObjectError, _
CMP_SRCNAME, "Unknown error sending mail memo."
End Select
Resume resExit
End Sub
'*
'add to list of attachments waiting. Any string is accepted, but
'files will be checked when memo is sent.
'*
Public Sub Attach(strFileToAttach As String)
Dim iTotal%
iTotal = UBound(sAttachments())
ReDim Preserve sAttachments(iTotal + 1)
sAttachments(iTotal + 1) = strFileToAttach
End Sub
'************************************************* ****************************
Option Compare Database
Public Sub SendMail()
' create a new notes mail
' by setting a variable
' which will reference our
' class file.
Dim oMemo As NotesMailMemo
' instantiate it (this could
' have been done above equally well
' by saying Dim oMemo As New NotesMailMemo)
Set oMemo = New NotesMailMemo
' let's set some properties
' of our new mail
With oMemo
' SendTo is the only property
' that is actually required
' to send a valid notes mail.
' All of the others after
' this are entirely discretionary.
' Group names and internet
' addresses will also work in
' the SendTo, cc and bcc fields
' if your domino server / notes
' client is setup correctly.
.SendTo = "recipient/group/site"
' optionally..
.CC = ""
.Bcc = ""
.Subject = "Test"
' the body field here is
' created from a String and not the
' rich-text format used by the
' notes client by default. If you
' need additional functionality here,
' you will have to amend the
' class file
.Body = "Text message"
' the signature file allows
' any text file to be appended to the
' body of the mail
.SigFile = ""
' couple of notes options
.Signed = False
.Encrypted = False
.Priority = "H" ' or "L" or "N"
' file attachments - you can call
' this method as often as you like
' for multiple files.
.Attach ""
.Attach ""
' deliver it..
.Send "username/group/site", "mypassword"
End With
End Sub