我正在尝试通过 Excel VBA 发送包含多个附件的 Outlook 电子邮件。
如果我指定一个附件/文件的路径,该代码就会起作用。如果我确切知道附件是什么,我还可以添加多个附件,但我不会。会有不同的计数和文件名。
我很想使用通配符发送,如下面的示例所示,但我认为我需要使用某种指向目录的循环。
我看了,但还没有看到任何适合我的情况的东西。
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 = "test@test.org"
.Subject = "test"
.HTMLBody = "test"
.Attachments.Add ("H:\test\Adj*.pdf")
'.DeleteAfterSubmit = True
.Send
End With
MsgBox "Reports have been sent", vbOKOnly
End Sub
最佳答案
试试这个
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 = "test@test.org"
.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
关于excel - 将多个文件或整个目录附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184217/