Add multiple attachments within Outlook Body

Harshil Mehta

Board Regular
Joined
May 14, 2020
Messages
85
Office Version
  1. 2013
Platform
  1. Windows
I am working on a file wherein I have to add multiple attachments within the Outlook mail body using the RICH TEXT function.

Below is the desired output:

Start of the email text

Part 1 Text
FILE 1 & FILE2

Part 2 Text
FILE 1 & FILE2

End of email text


However, my code seems to add the attachment files only after the Part2 Text. Could anyone please help me with the code?


VBA Code:
Sub Prepare_Drafts()
  
Dim OutApp As Object
Dim Default_Body As String
Dim shs As Worksheet
Dim File_Name As String
Dim p1 As Long, p2 As Long, p3 As Long


Application.ScreenUpdating = False



For Each shs In Sheets

shs.Activate
shs.Calculate

    If shs.Name <> "Dashboard" Then
    
       On Error Resume Next
       
       
       Set OutApp = GetObject(, "Outlook.Application")
       File_Name = shs.Range("D5").Value
  
 
 Default_Body = "Start of email text." & vbCr & vbCr & _
"Part 1 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"Part 2 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"End of email text."
    
    p1 = InStr(Default_Body, "*FILE1*")
    Default_Body = Replace(Default_Body, "*FILE1*", " ")
    
    p2 = InStr(Default_Body, "*FILE2*")
    Default_Body = Replace(Default_Body, "*FILE2*", " ")
 
 
 
       With OutApp.CreateItem(0)
           .BodyFormat = 3
           .To = shs.Range("D7").Value
           .CC = shs.Range("D11").Value
           .BCC = ""
           .Subject = shs.Range("D17").Value
           
           .Body = Default_Body
           .Attachments.Add "C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Jan.pdf" ', olByValue, p1, "file1"
           .Attachments.Add "C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Feb.pdf" ', olByValue, p2, "file2"
           
           
           '.SaveAs "C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\" & File_Name ', olTemplate
           .display
       End With
 
 
       Set OutApp = Nothing
  
   End If
  
Next shs



Sheets("Dashboard").Activate

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Untested!

I do it like this. The below code will cover up to 9 attachments, but you can increase or decrease that as needed.

VBA Code:
Dim myFileList(9) as String
Dim OutApp As Object
Dim Default_Body As String
Dim shs As Worksheet
Dim File_Name As String
Dim p1 As Long, p2 As Long, p3 As Long


Application.ScreenUpdating = False



For Each shs In Sheets

shs.Activate
shs.Calculate

    If shs.Name <> "Dashboard" Then
   
       On Error Resume Next
      
      
       Set OutApp = GetObject(, "Outlook.Application")
       File_Name = shs.Range("D5").Value
 
 
 Default_Body = "Start of email text." & vbCr & vbCr & _
"Part 1 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"Part 2 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"End of email text."
   
    p1 = InStr(Default_Body, "*FILE1*")
    Default_Body = Replace(Default_Body, "*FILE1*", " ")
   
    p2 = InStr(Default_Body, "*FILE2*")
    Default_Body = Replace(Default_Body, "*FILE2*", " ")

       myFileList(0) ="C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Jan.pdf" ', olByValue, p1, "file1"
       myFileList(1) ="C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Feb.pdf" ', olByValue, p2, "file2"

       With OutApp.CreateItem(0)
           .BodyFormat = 3
           .To = shs.Range("D7").Value
           .CC = shs.Range("D11").Value
           .BCC = ""
           .Subject = shs.Range("D17").Value
        
           .Body = Default_Body
              
                     For i = 0 To 8
                        If myFileList(i) <> Empty Then
                        .Attachments.Add myFileList(i)
                        Else
                        Exit For
                        End If
                    Next i
        
        
           '.SaveAs "C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\" & File_Name ', olTemplate
           .display
       End With

Set OutApp = Nothing
 
   End If
 
Next shs



Sheets("Dashboard").Activate

Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Thanks
Untested!

I do it like this. The below code will cover up to 9 attachments, but you can increase or decrease that as needed.

VBA Code:
Dim myFileList(9) as String
Dim OutApp As Object
Dim Default_Body As String
Dim shs As Worksheet
Dim File_Name As String
Dim p1 As Long, p2 As Long, p3 As Long


Application.ScreenUpdating = False



For Each shs In Sheets

shs.Activate
shs.Calculate

    If shs.Name <> "Dashboard" Then
  
       On Error Resume Next
     
     
       Set OutApp = GetObject(, "Outlook.Application")
       File_Name = shs.Range("D5").Value
 
 
 Default_Body = "Start of email text." & vbCr & vbCr & _
"Part 1 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"Part 2 Text" & vbCr & _
"*FILE1*" & "*FILE2*" & vbCr & vbCr & _
"End of email text."
  
    p1 = InStr(Default_Body, "*FILE1*")
    Default_Body = Replace(Default_Body, "*FILE1*", " ")
  
    p2 = InStr(Default_Body, "*FILE2*")
    Default_Body = Replace(Default_Body, "*FILE2*", " ")

       myFileList(0) ="C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Jan.pdf" ', olByValue, p1, "file1"
       myFileList(1) ="C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\Feb.pdf" ', olByValue, p2, "file2"

       With OutApp.CreateItem(0)
           .BodyFormat = 3
           .To = shs.Range("D7").Value
           .CC = shs.Range("D11").Value
           .BCC = ""
           .Subject = shs.Range("D17").Value
       
           .Body = Default_Body
             
                     For i = 0 To 8
                        If myFileList(i) <> Empty Then
                        .Attachments.Add myFileList(i)
                        Else
                        Exit For
                        End If
                    Next i
       
       
           '.SaveAs "C:\Users\" & ThisWorkbook.Sheets("Dashboard").Range("E5") & "\Desktop\" & File_Name ', olTemplate
           .display
       End With

Set OutApp = Nothing
 
   End If
 
Next shs



Sheets("Dashboard").Activate

Application.ScreenUpdating = True

End Sub
for the code but the output is still the same.
 
Upvote 0
What is the result? Is it just attaching one file or are you getting an error? Is this just for the text of the body? Try using .HTMLbody instead of just .body
 
Upvote 0
What is the result? Is it just attaching one file or are you getting an error? Is this just for the text of the body?
It is attaching both the files but not in the format I want. The desired format is shown above.
 
Upvote 0
Maybe I'm not understanding the wanted outcome. It looks like you are trying to attach a .pdf into the body of the email. Are you wanting to be able to view the .pdf within the body of the email or just have it as an attachment on the email to open separately? I know you can attach .jpg and other pic files within the body but not aware of a way to attach a .pdf within the body. Am I missing something?
 
Upvote 0
Yes, I want the attachments to be pasted within the body itself.
Maybe I'm not understanding the wanted outcome. It looks like you are trying to attach a .pdf into the body of the email. Are you wanting to be able to view the .pdf within the body of the email or just have it as an attachment on the email to open separately? I know you can attach .jpg and other pic files within the body but not aware of a way to attach a .pdf within the body. Am I missing something?
 
Upvote 0
Again, I'm not sure if that is possible. I'll wait for the others to weigh in. Good luck though.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top