I have a vbs script that I have been using to add a file from a folder as an attachment to an email, and then send it automatically through Outlook. Which works great.
The problem that I cannot figure out, is how to add 2 files which are in the same folder, to 1 email. I have been trying several things but the only thing I've been able to manage is adding "file 1" twice in an email, and "file 2" twice in another email.
I am a total novice at this so I apologize if this is some easy fix I can't figure out.
theFolder = "folder location"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(theFolder).Files
SendEmail objFSO.GetAbsolutePathName(objFile)
Next
Set objFSO = Nothing
Sub SendEmail(theFileName)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
objMail.Attachments.Add(theFileName)
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
CodePudding user response:
Pass the folder name as a parameter instead:
theFolder = "folder location"
SendEmail theFolder
Sub SendEmail(folderName)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(folderName).Files
objMail.Attachments.Add(objFile.ParentFolder & "\" & objFile.Name)
Next
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
Set objFSO = Nothing
End Sub
CodePudding user response:
You need to call the Attachments.Add
for each file in the folder. So, your code may look like that:
theFolder = "folder location"
Sub SendEmail()
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(theFolder).Files
objMail.Attachments.Add(objFSO.GetAbsolutePathName(objFile))
Next
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
Set objFSO = Nothing
End Sub