vba 将多个文件或整个目录附加到电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26184217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attach multiple files or entire directory to email
提问by gfuller40
I'm trying to send an Outlook email with multiple attachments via Excel VBA.
我正在尝试通过 Excel VBA 发送带有多个附件的 Outlook 电子邮件。
The code works if I specify the path to one attachment/file. I can also add multiple attachments if I know exactly what they are, but I will not. There will be different counts as well as file names.
如果我指定一个附件/文件的路径,则代码有效。如果我确切地知道它们是什么,我也可以添加多个附件,但我不会。会有不同的计数以及文件名。
I would love to send using a wildcard as shown in my example below but I think I'll need to use some sort of loop pointing at a directory.
我很想使用通配符发送,如下面的示例所示,但我认为我需要使用某种指向目录的循环。
I looked but I am yet to see anything that works with my situation.
我看了看,但我还没有看到任何适合我的情况的东西。
Private Sub Command22_Click()
Dim mess_body As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "[email protected]"
.Subject = "test"
.HTMLBody = "test"
.Attachments.Add ("H:\test\Adj*.pdf")
'.DeleteAfterSubmit = True
.Send
End With
MsgBox "Reports have been sent", vbOKOnly
End Sub
回答by Siddharth Rout
Is this what you are trying? (UNTESTED)
这是你正在尝试的吗?(未经测试)
Private Sub Command22_Click()
Dim mess_body As String, StrFile As String, StrPath As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
'~~> Change path here
StrPath = "H:\test\"
With MailOutLook
.BodyFormat = olFormatRichText
.To = "[email protected]"
.Subject = "test"
.HTMLBody = "test"
'~~> *.* for all files
StrFile = Dir(StrPath & "*.*")
Do While Len(StrFile) > 0
.Attachments.Add StrPath & StrFile
StrFile = Dir
Loop
'.DeleteAfterSubmit = True
.Send
End With
MsgBox "Reports have been sent", vbOKOnly
End Sub

