-1

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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
YaBe1742
  • 1
  • 1
  • What value does the `objFSO.GetAbsolutePathName(objFile)` call return each time? – Eugene Astafiev Feb 14 '22 at 13:59
  • @EugeneAstafiev it "Returns a complete and unambiguous path from a provided path specification." - [`GetAbsolutePathName` Method](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/zx1xa64f(v=vs.84)). – user692942 Feb 14 '22 at 15:32
  • 1
    Does this answer your question? [VBScript to loop through all files in a folder](https://stackoverflow.com/questions/16665748/vbscript-to-loop-through-all-files-in-a-folder). First get your loop right, then you'll be able to incorporate it into adding them as attachments. – user692942 Feb 14 '22 at 15:36

2 Answers2

0

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.Path)
    Next

    objMail.Send
    Set objMail = Nothing
    Set objOutlook = Nothing
    Set objFSO = Nothing

End Sub
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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
user692942
  • 16,398
  • 7
  • 76
  • 175
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45