将文件夹中的多个文件作为附件添加到电子邮件 vbs

Add multiple files from folder as attachment to email vbs

我有一个 vbs 脚本,我一直使用该脚本将文件夹中的文件作为电子邮件的附件添加,然后通过 Outlook 自动发送。效果很好。

我无法弄清楚的问题是如何将同一文件夹中的 2 个文件添加到 1 封电子邮件。我一直在尝试几件事,但我唯一能够做到的是在一封电子邮件中添加两次“文件 1”,在另一封电子邮件中添加两次“文件 2”。

我在这方面完全是个新手,所以如果这是我无法弄清楚的简单修复方法,我深表歉意。

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

改为将文件夹名称作为参数传递:

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

您需要为文件夹中的每个文件调用 Attachments.Add()。因此,您的代码可能如下所示:

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