vba - 使用 VBA 从 Excel 工作表发送多个附件

标签 vba email excel outlook

我有现有代码可以从 Excel 文件中的工作表发送邮件 -

Sub CreateMail()

    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngSubject As Range
    Dim rngBody As Range
    Dim rngAttach As Range

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    Application.ScreenUpdating = False
    Worksheets("Mail List").Activate

    With ActiveSheet
        Set rngTo = .Range("B1")
        Set rngSubject = .Range("B2")
        Set rngBody = .Range("B3")
        Set rngAttach = .Range("B4")

    End With

    With objMail
        .To = rngTo.Value
        .Subject = rngSubject.Value
        .body = rngBody.Value
        .Attachments.Add rngAttach.Value
        .display 'Instead of .Display, you can use .Send to send the email _
                    or .Save to save a copy in the drafts folder
    End With

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngSubject = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing

End Sub

但是,我想包含一些附件,因此 Set rngAttach = .Range("B4") 对此没有帮助。

对此有什么帮助吗? 提前致谢!

最佳答案

将 .Attachments.Add 语句包含在循环中。像下面这样的东西可能会起作用

    For i = 4 To 6
      .Attachments.Add Range("B" & i).Value
    Next i 

关于vba - 使用 VBA 从 Excel 工作表发送多个附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251868/

相关文章:

excel - 循环遍历指定文件夹中的所有 Excel 文件并从特定单元格中提取数据的代码

excel - 如何在Excel中制作一个定期执行的宏?

php - 为什么 PHP imap_headerinfo() 函数在大邮箱上慢得多?

sql-server - 要求使用 excel 报告中的列数识别报告

excel - 如何创建工作表的动态 View (过滤器)?

Excel VBA根据列表框选择填充列表框

vba - 将工作表单元格的值分配给常量

Excel/VBA - 如果网络连接不存在则中止脚本

php - Linux - 使用 PHP Mail 从 SMTP 服务器发送

azure - 为 Azure 警报配置电子邮件模板