andyonweb
07-22-2023, 01:21 AM
Hi all,
this function allow to send by email all the files presents in a folder.
Compiling the function the result is myFiles As Outlook.File not defined from user. Can u help me to fix?
Thanks!!!!
Sub SendExcelFiles()
'Declare variables
Dim myFolder As Outlook.Folder
Dim myFiles As Outlook.File
Dim myEmailAddress As String
Dim mySubject As String
Dim myCell As Range
'Set the folder variable
Set myFolder = Application.GetFolder("C:\Users\[Your User Name]\Desktop\Excel Files")
'Get all the files in the folder
Set myFiles = myFolder.Files
'Loop through the files
For Each myFile In myFiles
'Get the email address from the cell
Set myCell = myFile.Sheets(1).Range("A1")
myEmailAddress = myCell.Value
'Get the subject of the email
mySubject = myFile.Name.Replace(".xlsx", "")
'Create a new email message
Dim myMail As Outlook.MailItem
Set myMail = Application.CreateItem(olMailItem)
'Set the sender's email address
myMail.SenderEmailAddress = "[Your Email Address]"
'Set the recipient's email address
myMail.To = myEmailAddress
'Set the subject of the email
myMail.Subject = mySubject
'Set the body of the email
myMail.Body = "This is an email message with an Excel file attached."
'Attach the file to the email
myMail.Attachments.Add myFile
'Send the email
myMail.Send
Next myFile
End Sub
this function allow to send by email all the files presents in a folder.
Compiling the function the result is myFiles As Outlook.File not defined from user. Can u help me to fix?
Thanks!!!!
Sub SendExcelFiles()
'Declare variables
Dim myFolder As Outlook.Folder
Dim myFiles As Outlook.File
Dim myEmailAddress As String
Dim mySubject As String
Dim myCell As Range
'Set the folder variable
Set myFolder = Application.GetFolder("C:\Users\[Your User Name]\Desktop\Excel Files")
'Get all the files in the folder
Set myFiles = myFolder.Files
'Loop through the files
For Each myFile In myFiles
'Get the email address from the cell
Set myCell = myFile.Sheets(1).Range("A1")
myEmailAddress = myCell.Value
'Get the subject of the email
mySubject = myFile.Name.Replace(".xlsx", "")
'Create a new email message
Dim myMail As Outlook.MailItem
Set myMail = Application.CreateItem(olMailItem)
'Set the sender's email address
myMail.SenderEmailAddress = "[Your Email Address]"
'Set the recipient's email address
myMail.To = myEmailAddress
'Set the subject of the email
myMail.Subject = mySubject
'Set the body of the email
myMail.Body = "This is an email message with an Excel file attached."
'Attach the file to the email
myMail.Attachments.Add myFile
'Send the email
myMail.Send
Next myFile
End Sub