I am using the following vba code to try and send an email from IBM Notes with an attachment.
Here is my code:
Sub Send_Email()
Dim answer As Integer
answer = MsgBox("Are you sure you want to Send All Announcements?", vbYesNo + vbQuestion, "Notice")
If answer = vbNo Then
Exit Sub
Else
'Define Parameters for Email
Dim s As Object
Dim db As Object
Dim body As Object
Dim bodyChild As Object
Dim header As Object
Dim stream As Object
Dim host As String
Dim MailDoc As Object
'Define Sheet Parameters
Dim i As Long
Dim j As Long
Dim server, mailfile, user, usersig As String
Dim LastRow As Long, ws As Worksheet
LastRow = Worksheets(1).Range("F" & Rows.Count).End(xlUp).Row 'Finds the last used row
j = 18
'Start a session of Lotus Notes
Set Session = CreateObject("Notes.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
Set db = Session.CurrentDatabase
Set stream = Session.CreateStream
' Turn off auto conversion to rtf
Session.ConvertMime = False
With ThisWorkbook.Worksheets(1)
For i = 18 To LastRow
' Create message
Set MailDoc = db.CreateDocument
MailDoc.Form = "Memo"
'Set From
MailDoc.SendTo = Range("Q" & i).value
MailDoc.SentBy = "[email protected]"
MailDoc.tmpDisplaySentBy = "[email protected]"
MailDoc.FROM = "[email protected]"
MailDoc.SendFrom = "[email protected]"
MailDoc.Principal = "Food Specials <mailto:[email protected]>"
MailDoc.Subject = "Promotion Announcement for week " & Range("I8").value & ", " & Range("T8").value & " - Confirmation required"
'MailDoc.SendTo = Range("Q" & i).value
'Call MailDoc.ReplaceItemValue("CopyTo", "[email protected]")
MailDoc.SaveMessageOnSend = True
' Create the body to hold HTML and attachment
Set body = MailDoc.CreateMIMEEntity
'Child mime entity which is going to contain the HTML which we put in the stream
Set bodyChild = body.CreateChildEntity()
Call stream.WriteText(strbody)
Call bodyChild.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_NONE)
Call stream.Close
Call stream.Truncate
' Get the attachment file name
filename = Range("F" & i).value
'A new child mime entity to hold a file attachment
Set header = bodyChild.CreateHeader("Content-Type")
Call header.SetHeaderVal("multipart/mixed")
Set header = bodyChild.CreateHeader("Content-Disposition")
Call header.SetHeaderVal("attachment; filename=" & filename)
Set header = bodyChild.CreateHeader("Content-ID")
Call header.SetHeaderVal(filename)
Set stream = Session.CreateStream()
Call bodyChild.SetContentFromBytes(stream, "application/msexcel", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments.
'Call bodyChild.SetContentFromBytes(1454, "", Range("F" & i).value, "Attachment")
'Send the email
Call MailDoc.Send(False)
Session.ConvertMime = True ' Restore conversion
j = j + 1
Next i
End With
'Clean Up the Object variables - Recover memory
Application.CutCopyMode = False
MsgBox "Success!" & vbNewLine & "Announcements have been sent."
End If
End Sub
It doesn't seem to want to attach any attachment or send. I get an error: Object Variable or with block variable not set on this line:
Call header.SetHeaderVal("multipart/mixed")
Please can someone show me where i am going wrong?
EDIT 2:
Ok i managed to get rid of the errors and get the email to send.
However, it's not sending the attachment properly. All i see is something like this:
Here is the code:
Sub Send_Email()
Dim answer As Integer
answer = MsgBox("Are you sure you want to Send All Announcements?", vbYesNo + vbQuestion, "Notice")
If answer = vbNo Then
Exit Sub
Else
'Define Parameters for Email
Dim s As Object
Dim db As Object
Dim body As Object
Dim bodyChild As Object
Dim header As Object
Dim stream As Object
Dim host As String
Dim MailDoc As Object
'Define Sheet Parameters
Dim i As Long
Dim j As Long
Dim server, mailfile, user, usersig As String
Dim LastRow As Long, ws As Worksheet
LastRow = Worksheets(1).Range("F" & Rows.Count).End(xlUp).Row 'Finds the last used row
j = 18
'Start a session of Lotus Notes
Set Session = CreateObject("Notes.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
Set db = Session.CurrentDatabase
Set stream = Session.CreateStream
' Turn off auto conversion to rtf
Session.ConvertMime = False
With ThisWorkbook.Worksheets(1)
For i = 18 To LastRow
' Create message
Set MailDoc = db.CreateDocument
MailDoc.Form = "Memo"
'Set From
MailDoc.SendTo = Range("Q" & i).value
MailDoc.SentBy = "[email protected]"
MailDoc.tmpDisplaySentBy = "[email protected]"
MailDoc.FROM = "[email protected]"
MailDoc.SendFrom = "[email protected]"
MailDoc.Principal = "Food Specials <mailto:[email protected]>"
MailDoc.Subject = "Promotion Announcement for week " & Range("I8").value & ", " & Range("T8").value & " - Confirmation required"
'MailDoc.SendTo = Range("Q" & i).value
'Call MailDoc.ReplaceItemValue("CopyTo", "[email protected]")
MailDoc.SaveMessageOnSend = True
' Create the body to hold HTML and attachment
Set body = MailDoc.CreateMIMEEntity
'Child mime entity which is going to contain the HTML which we put in the stream
Set bodyChild = body.CreateChildEntity()
Call stream.WriteText(strbody)
Call bodyChild.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_NONE)
Call stream.Close
Call stream.Truncate
filename = Range("F" & i).value
'A new child mime entity to hold a file attachment
Set bodyChild = body.CreateChildEntity()
Set header = bodyChild.CreateHeader("Content-Type")
header.SetHeaderVal ("multipart/mixed")
Set header = bodyChild.CreateHeader("Content-Disposition")
header.SetHeaderVal ("attachment; filename=" & filename)
Set header = bodyChild.CreateHeader("Content-ID")
header.SetHeaderVal (filename)
Set stream = Session.CreateStream()
Call bodyChild.SetContentFromBytes(stream, "application/msexcel", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments.
'Send the email
Call MailDoc.Send(False)
Session.ConvertMime = True ' Restore conversion
j = j + 1
Next i
End With
'Clean Up the Object variables - Recover memory
Application.CutCopyMode = False
MsgBox "Success!" & vbNewLine & "Announcements have been sent."
End If
End Sub
Please can someone show me why my excel file is not attaching correctly? Thanks
