VBA Add multiple attachments to an email

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
I have the below code that converts and saves 2 worksheets to pdfs and then creates an email.

What I would like to add is code that attaches every document from column C from its corresponding directory in column D.
Column C and D may change (become longer or shorter) depending on what files the user wants to attach.

Any help with modifying this code would be greatly appreciated.

OPS 068 Rev 17 and OPS 069 Rev 17.xlsm
ABCD
1Customer:E MetalsDocuments to AttachDirectories
2Contact:Carole SmithWTC amo.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
3Email:Carole.Smith@notreal.comWTC comp.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
4ISO TRC.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
5RC Wpg.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
6RR Wpg.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
7RRT Wpg.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
8OPS 068.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
9OPS 069.pdfX:\ADMINISTRATION\QAEng\Carla's Documents\Project\
Input


VBA Code:
Sub Email()
'
' Email Macro
'
Dim Path As String
Dim filename As String

Path = Sheets("Input").Range("D8")
filename = Sheets("Input").Range("C8")

Sheets("OPS 068").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True

Dim Path2 As String
Dim filename2 As String

Path2 = Sheets("Input").Range("D9")
filename2 = Sheets("Input").Range("C9")

Sheets("OPS 069").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path2 & filename2 & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
'
Sheets("Input").Select

  Dim outlookApp As Object, MItem As Object
  Set outlookApp = CreateObject("Outlook.Application")
  Set MItem = outlookApp.CreateItem(0)
  With MItem
    .To = Sheets("Input").Range("B3")
    .Subject = "Supplier Survey"
    .Body = "Good Afternoon " & Sheets("Input").Range("B2") & vbCrLf & vbCrLf & "Please find attached our documents." & vbCrLf & vbCrLf & "Please let us know if you require anything further"
    .Display
  End With


End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Figured it out. I combined Column C with D in Column E and modified it as follows:

VBA Code:
Sub Email()
'
' Email Macro
'
Application.ScreenUpdating = False

Dim Path As String
Dim filename As String

Path = Sheets("Input").Range("D8")
filename = Sheets("Input").Range("C8")

Sheets("OPS 068").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False

Dim Path2 As String
Dim filename2 As String

Path2 = Sheets("Input").Range("D9")
filename2 = Sheets("Input").Range("C9")

Sheets("OPS 069").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path2 & filename2 & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False
'
Sheets("Input").Select

  Dim outlookApp As Object, MItem As Object
  Set outlookApp = CreateObject("Outlook.Application")
  Set MItem = outlookApp.CreateItem(0)
  Set ws = Sheets("Input")
 
  With ws
    Set rngAttach = ws.Range(ws.[E2], ws.Cells(Rows.Count, "E").End(xlUp))
End With

  With MItem
    .To = ws.Range("B3")
    .Subject = "Supplier Survey"
    .Body = "Good Afternoon " & ws.Range("B2") & vbCrLf & vbCrLf & "Please find attached our documents." & vbCrLf & vbCrLf & "Please let us know if you require anything further"
    For Each rng1 In rngAttach.Cells
        If Len(Dir(rng1)) > 0 Then .Attachments.Add rng1.Value
    Next
    .Display
  End With

Application.ScreenUpdating = True

End Sub

Would still like to see other solutions though if anyone wishes to post.

Thank you for reading this! :)
 
Upvote 0
Solution
That looks fine. I would suggest changing this line though:
VBA Code:
' Set rngAttach = ws.Range(ws.[E2], ws.Cells(Rows.Count, "E").End(xlUp))
Set rngAttach = ws.Range(ws.[E2], ws.Cells(ws.Rows.Count, "E").End(xlUp))
ws.[E2] is fine but "E2" would have worked too.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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