Home » excel » vba – How to add multiple attachments from a column from excel that has any value (file path) in it

vba – How to add multiple attachments from a column from excel that has any value (file path) in it

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to create a VBA code that will be able to tell excel to add multiple attachments from a specific column that has any value (file path) in it.

E.G. My Excel Column BD2:BD2000 contains a list of email attachmments only and I want excel to select all values from cells BD2:BD20000 (If they are not empty, and Which are basically file paths) and attach it as multiple attachments to my outlook email.

However, I can’t get this to work.

This is what I have so far:

Private Sub ToggleButton3_Click()

 Dim OutApp As Object
    Dim OutMail As Object

    Set emailRng = Worksheets("Workings").Range("BC2:BC2000")
    Set FilepathRng = Worksheets("Workings").Range("BD2:BD2000")


    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value

    Next

    sTo = Mid(sTo, 2)

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Next

    sTo = Mid(sTo, 2)


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = ComboBox17.Value
        .CC = sTo
        .BCC = ""
        .Subject = TextBox18.Value
        .Body = "Hi there"
        .Attachments.add = FilepathRng


    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing


End Sub

Appreciate if anyone would be so kind to help. I’m new to VBA and I’m trying to find simple solutions for this so I would appreciate any detailed and easy to understand/advice/solutions any one can give.

Thank you!

How to&Answers:

This code will attach all the file paths in Range “BC2:BC2000”.

Warning:

It’s not a good approach to add that many files in one Email. Your system may hang. To add 2000 files change the range in For Loop accordingly.

Private Sub ToggleButton3_Click()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim cl As Range
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Set emailRng = Worksheets("Workings").Range("BC2:BC2000")

    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value

    Next

    sTo = Mid(sTo, 2)

    With OutMail
        .To = ComboBox17.Value
        .CC = sTo
        .BCC = ""
        .Subject = TextBox18.Value
        .Body = "Hi there"

        For Each cl In Worksheets("Workings").Range("BD2:BD2000")
            If Len(cl.Value) > 1 Then
            .Attachments.Add cl.Value
            End If
        Next

        .Display

    End With


    Set OutMail = Nothing
    Set OutApp = Nothing


End Sub

Answer:

First you need to fill an array with your paths, like this for example:

With ThisWorkbook.Sheets("Workings")
    LastRow = .Cells(.Rows.Count, "BD").End(xlUp).Row
    ReDim myFileList(1 To LastRow - 2)
    i = 1
    For Each C In .Range("BD2:BD" & LastRow)
        myFileList(i) = C
        i = i + 1
    Next C
End With

Then to add the attachments you need to do like this:

    For i = LBound(myFileList) To UBound(myFileList)
        .Attachments.Add myFileList(i)
    Next i