VBA to email multiple addresses with unique attachments

MM2024

New Member
Joined
Jun 19, 2024
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I have used the code in the below thread successfully but I need to add an attachment to each email. The filepath for each attachment is in excel (column C) but I can't seem to get the code to pick up the required cell. Please help.


 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This is the code I'm currently using. Where I have .Attachments.Add Range("C2").Value I don't actually just want to pick up a fixed cell as it currently is, I want to pick up the cell (3rd column in my spreadsheet) associated with the row being emailed.

VBA Code:
Sub CreateEmails()
    Dim OutApp As Object, OutMail As Object, v As Variant, i As Long, rng As Range
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    Set OutApp = CreateObject("Outlook.Application")
    
    With CreateObject("scripting.dictionary")
      For i = LBound(v) To UBound(v) 'loops through rows
         If Not .exists(v(i, 1)) Then
            .Add v(i, 1), Nothing
            With ActiveSheet
                .Range("A1").AutoFilter 1, v(i, 1)
                Set rng = .AutoFilter.Range
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .To = v(i, 2)
                    .Subject = Range("K1").Value & " Invoice " & v(i, 1)
                    .HTMLBody = "Insert your message here." & "<br>" & "More message"
                    .Display
                    .Attachments.Add Range("C2").Value
                End With
            End With
        End If
      Next i
      ActiveSheet.Range("A1").AutoFilter
   End With
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")
    TempWB.Close savechanges:=False
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Upvote 0
Please post what the value of cell C2 looks like. Also, the macro filters the data based on the unique values in column A. However, you don't include the filtered data in your email. Is this the case?
 
Upvote 0
Thanks for your reply. Here are the example values being used. (I'm not sure what you mean by 'filtered' data sorry)

1718919638460.png
 
Upvote 0
To add to my post, C2 is the file path: C:\Users\an\Documents\doc.docx and this is unique to each user in the table.
 
Upvote 0
Think I might have just figured it out.

Up the top I changed .Resize(, 2) to .Resize(, 3) and then I was able to successfully change the .Attachments.Add Range("C2").Value to .Attachments.Add v(i, 3) and it is working.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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