Hello, I have some code that I had thought worked fine but turns out doesn't. My code sends an email using VBA through lotus notes, it first attaches a PDF document to the email via RichText, which works and the PDF is attached. Then it uses MIME to attach a HTML to the body so I can get some lovely formatted text before it sends the whole email to users that are provided via Excel table. What is strange that in the sent box it all seems to work fine and the PDF attachment is there as well as the HTML body.
I have checked however and for external users they don't get the PDF attachment. If I remove the MIME part of the code the PDF is attached fine but I can't lose the HTML body. Question then, can I fix this to use both RichText embedded attachment and a MIME body or otherwise how do I use MIME to embed another PDF attachment alongside the HTML body?
I have checked however and for external users they don't get the PDF attachment. If I remove the MIME part of the code the PDF is attached fine but I can't lose the HTML body. Question then, can I fix this to use both RichText embedded attachment and a MIME body or otherwise how do I use MIME to embed another PDF attachment alongside the HTML body?
VBA Code:
Public Sub COM_Email_Send()
Dim NSession As Object
Dim NMailDb As Object
Dim NDocument As Object
Dim NBody As Object
Dim NChild As Object
Dim Nstream As Object
Dim RichTextHeader As Object
Dim i As Long
Dim Row As Long
Dim Recipient As String
Dim File As String
Dim attachmentFile As String
Dim Data As String
Dim AttachedOb As Object
Dim EmbedOb As Object
Dim NHeader As Object
Dim strFileType As Variant
Dim MIMEDoc As Object
Set NSession = CreateObject("Lotus.NotesSession")
Call NSession.Initialize("password")
Set NMailDb = NSession.GetDatabase("directory", "server")
If Not NMailDb.IsOpen = True Then
Call NMailDb.Open
End If
Row = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To Row
Recipient = Worksheets("Sheet1").Range("B" & i)
If Recipient <> "" Then
File = Worksheets("Sheet1").Range("A" & i).Value
attachmentFile = "Directory" & File
Data = Format(Now(), "dd/mm/yyyy")
Set NDocument = NMailDb.CreateDocument
Set Nstream = NSession.CreateStream
Call NDocument.replaceitemvalue("Form", "Memo")
Call NDocument.replaceitemvalue("SendTo", Recipient)
Call NDocument.replaceitemvalue("Subject", "Please see your clearance documents attached " & Data)
Call NDocument.replaceitemvalue("Sender", "noreply@test.com")
If attachmentFile <> "" Then
Set AttachedOb = NDocument.Createrichtextitem("attachmentFile")
Set EmbedOb = AttachedOb.embedobject(1454, "", attachmentFile, "")
End If
Call Nstream.Open("Directory\HTML BODY.htm")
Set NBody = NDocument.CreateMIMEEntity '("memo")
Set RichTextHeader = NBody.CreateHeader("Content-Type")
Call RichTextHeader.SetHeaderVal("multipart/mixed")
Set MIMEDoc = NBody.CreateChildEntity()
Call MIMEDoc.SetContentFromBytes(Nstream, "text/html", ENC_IDENTITY_BINARY)
Call Nstream.Close
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
End Sub