使用vbscript通过电子邮件发送文件夹/文件

时间:2015-04-20 14:43:23

标签: shell vbscript

基本上我想通过电子邮件发送文件夹中的文件。该文件是在执行某个程序后动态生成的。但问题是如何附加动态生成的文件(名称不断变化

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "to@gmail.com"
objMail.cc = "from@gmail.com"
objMail.Subject = "Test Mail Subject"
objMail.Body = "Test mail body"
objMail.Attachments.Add("path")
objMail.Send 
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing

我可以获取文件夹的路径,但我如何能够选择在文件夹中附加任何文件。我可以邮寄整个文件夹(通过压缩它)。如果是这样的话。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Const ForReading = 1
Const ForWriting = 2

theFolder = "FolderPathGoesHere"
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 = "to@gmail.com"
    objMail.cc = "from@gmail.com"
    objMail.Subject = "Test Mail Subject"
    objMail.Body = "Test mail body"
    objMail.Attachments.Add(theFileName)
    objMail.Send 
    objOutlook.Quit
    Set objMail = Nothing
    Set objOutlook = Nothing

End Sub