PDA

View Full Version : Solved: Multiple attachments - email



debauch
06-27-2007, 09:15 AM
Hello VBA Xpress group!

I found the below code, to attach a file to a Lotus notes email. I tried declaring 'stAttachment2' in an attempt to attach a 2nd file. I am hoping to in the end, have several attachments for each email. However, based on my attempt below, the code still only sends one attachment. Is there a way to change this easily you think?

Private Sub CommandButton1_Click()
Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim obAttachment As Object, EmbedObject As Object
Dim stSubject As Variant
Dim stAttachment As String
Dim stAttachment2 As String 'added new
Dim vaRecipient As Variant, vaMsg As Variant

Const EMBED_ATTACHMENT As Long = 1454
Const stTitle As String = "Active workbook status"
Const stMsg As String = "The active workbook must first be saved " & vbCrLf _
& "before it can be sent as an attachment."
'Check if the active workbook is saved or not
'If the active workbook has not been saved at all.
If Len(ActiveWorkbook.Path) = 0 Then
MsgBox stMsg, vbInformation, stTitle
Exit Sub
End If
'If the changes in the active workbook have been saved or not.
If ActiveWorkbook.Saved = False Then
If MsgBox("Do you want to save the changes before sending?", _
vbYesNo + vbInformation, stTitle) = vbYes Then _
ActiveWorkbook.Save
End If

'recipiants
vaRecipient = "email@work.com"

vaMsg = "body of email text ...."

stSubject = "Week Report Yada yada .."

stAttachment = "c:\test.txt"
stAttachment2 = "c:\test2.txt"

'Instantiate the Lotus Notes COM's Objects.
Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")
'If Lotus Notes is not open then open the mail-part of it.
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
'Create the e-mail and the attachment.
Set noDocument = noDatabase.CreateDocument
Set obAttachment = noDocument.CreateRichTextItem("stAttachment, stAttachment2")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment, stAttachment2)
'Add values to the created e-mail main properties.
With noDocument
.Form = "Memo"
.SendTo = vaRecipient
.Subject = stSubject
.Body = vaMsg
.SaveMessageOnSend = True
End With
'Send the e-mail.
With noDocument
.PostedDate = Now()
.Send 0, vaRecipient
End With

'Release objects from the memory.
Set EmbedObject = Nothing
Set obAttachment = Nothing
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing

'Activate Excel for the user.
AppActivate "Microsoft Excel"
'MsgBox "The e-mail has successfully been created and distributed.", vbInformation

End Sub

debauch
06-27-2007, 10:05 AM
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", tAttachment2)


probably not the cleanest way to do it , but it worked. I will mark solved but would like to see if anyone has a take on it?