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
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