Macro to Attach File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below ro create an email (this works perfectly) ,and attach a file



The file is not being attached
It would be appreciated if someone could amend my code

Code:
 Sub EmailandAttach_File()
    Dim objOutlook As Object
    Dim objMail As Object
    Dim strAttachmentPath As String
    Dim strWildcard As String
    Dim strEmailAddress As String
    Dim strSubject As String
    Dim strBodyText As String
    
    ' Get the values from the Email sheet
    strSubject = ThisWorkbook.Sheets("Email").Range("B1").Value
    strBodyText = ThisWorkbook.Sheets("Email").Range("B2").Value
    strEmailAddress = ThisWorkbook.Sheets("Email").Range("S1").Value
    
    ' Set the path to the folder containing the file you want to attach
    strAttachmentPath = "C:\RECONS\Sales\"
    
    ' Set the wildcard to match the file you want to attach
    strWildcard = "Sales Cheque*.xlsx"
    
    ' Create an instance of the Outlook application
    Set objOutlook = CreateObject("Outlook.Application")
    
    ' Create a new email message
    Set objMail = objOutlook.CreateItem(0)
    
    ' Set the recipient and subject of the email message
    objMail.To = strEmailAddress
    objMail.Subject = strSubject
    
    ' Set the body text of the email message
    objMail.Body = strBodyText
    
    ' Attach the file
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strAttachmentPath)
    
    For Each objFile In objFolder.Files
        If InStr(objFile.Name, strWildcard) = 1 Then
            objMail.Attachments.Add objFile.Path
        End If
    Next objFile
    
    ' Display the email message
    objMail.Display
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.
Let's try:


Rich (BB code):
If objFile.Name Like strWildcard Then
                objMail.Attachments.Add objFile.Path
End If
 
Upvote 0
Solution
Many Thanks KOKOSEK. Your code has sorted out the issue
 
Upvote 0

Forum statistics

Threads
1,214,884
Messages
6,122,082
Members
449,064
Latest member
MattDRT

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