excel - 将多个文件或整个目录附加到电子邮件

标签 excel vba outlook

我正在尝试通过 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/

相关文章:

vba - 展望事件 : On change 'From Address'

html - 与时事通讯文本不同颜色的下划线。(与 outlook 兼容)

excel - 在 VBScript 中调用 Excel 工作表

vba - 在 Excel VBA 中访问命名单元格的值(内容)

python - 从 Python 打开 Excel 应用程序

ms-access - 错误代码 3021 bof 或 eof 为真或当前记录已被删除

vb.net - 保存“记住我”按钮? (VB.net)

sql - 带有 SQL : No row returned when Where clause specified 的 Excel VBA

excel - 为什么我会遇到这个 If 语句问题?

excel - VBA 在 If 语句中采用错误的分支 - 严重的编译器错误?