VBA: Send Outlook Email & Attach PDF

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hi Excel Community,

Below is a screenshot of the structure of my Excel file.

1712584421959.png


Each row is a separate email and the location of the file is in column E and there will only ever be one attachment.

I took this code from another thread and tried modifying it as it pulled ALL the PDFs from the file folder where as I already have the filepath of the attachment.

Here is my code:
SQL:
Sub SendMail()

    ActiveWorkbook.RefreshAll
    
    Dim objOutlook As Object
    Dim objMail As Object
    Dim ws As Worksheet
    Dim fileName As String
    Dim cell As Range
    

    Set objOutlook = CreateObject("Outlook.Application")
    Set ws = ActiveSheet
    
  For Each cell In ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))


    Set objMail = objOutlook.CreateItem(0)


        With objMail
            .To = cell.Value
            .Subject = cell.Offset(0, 2).Value
            .CC = cell.Offset(0, 1).Value
            .Body = cell.Offset(0, 3).Value
            fileName = Dir(cell.Offset(0, 4).Value)
                        While fileName <> vbNullString
                            .Attachments.Add cell.Offset(0, 4).Value
                            fileName = Dir()
                        Wend
            .Display
        End With

        Set objMail = Nothing
    Next cell

    Set ws = Nothing
    Set objOutlook = Nothing

End Sub

Any help would be appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi larinda4,

Untested but I think you just need to remove or comment out this block...

VBA Code:
Filename = Dir(cell.Offset(0, 4).Value)
        While Filename <> vbNullString
            .Attachments.Add cell.Offset(0, 4).Value
            Filename = Dir()
        Wend

...and use this:

VBA Code:
 .Attachments.Add cell.Offset(0, 4).Value

Hope that helps,

Robert
 
Upvote 0

Forum statistics

Threads
1,215,193
Messages
6,123,560
Members
449,108
Latest member
rache47

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