Wild card VBA for file attachment

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I found this bit of code on the internet (thanks ron) to send an email from Excel and attach a file. This works fine when I specify the path and file name (cell C16). In C16 I'll have the following 'Z:\ISB Data\Data Request Documents\Email files\Text.xlsx'. I have tried having 'Z:\ISB Data\Data Request Documents\Email files\*.xlsx', so it would attach any xlsx file in there ( my intention is to only have one file there at a time and place new files there as they are needed etc) but this did not work.

Code:
 Sub EmailBasedOnCells3()
    
'For Tips see: [URL]http://www.rondebruin.nl/win/winmail/Outlook/tips.htm[/URL]
'Working in Office 2000-2013
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim strbody As String
    Dim rngAttach As Range
    
    'Set range for file attachment, cell should contain the path and file name
    With ActiveSheet
    Set rngAttach = .Range("c16")
    End With
    'Body of email is based on cells E2 to E60 or whatever range we end up using
     For Each cell In Range("E2:E60")
         strbody = strbody & cell.Value & vbNewLine
     Next
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    On Error GoTo cleanup
    For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "D").Value) = "yes" Then
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .CC = [EMAIL="someemail@blahblah.com"]someemail@blahblah.com[/EMAIL]
                .Subject = ThisWorkbook.Sheets("Email to Director").Range("c3").Value
                .Body = "Dear " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
                'You can add files also like this
               .Attachments.Add rngAttach.Value
                .Display  'Or use Send, Send will send the email without you seeing it, safer to use Display
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell
cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Any help or guidance is much appreciated

Cheers
Haydn
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Haydn

You need the full path and filename for the attachment.

To get that you can use Dir, something like this.
Code:
strFileName = Dir(rngAttach.Value)

' other code

.Attachments.Add Replace(rngAttach.Value, "*.*","") & strFileName

' rest of code
 
Upvote 0
Thanks for the response guys, I will give it a go and let you know how it went.

Cheers
Haydn
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,480
Members
452,192
Latest member
FengXue

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