【问题标题】:Sending emails with multiple attachments to multiple recipients将带有多个附件的电子邮件发送给多个收件人
【发布时间】:2021-04-10 01:44:27
【问题描述】:

我在网上找到了一个代码,它可以向多个收件人发送电子邮件,每封电子邮件都附加一个文件。

我想去一个特定的文件夹,附加文件夹中的所有 PDF 文件,然后去另一个文件夹,为下一个电子邮件收件人做同样的事情。

图像显示了工作表的结构。我正在使用 Office 365。

Sub SendMail()

    ActiveWorkbook.RefreshAll
    
    Dim objOutlook As Object
    Dim objMail As Object
    Dim ws As Worksheet

    Set objOutlook = CreateObject("Outlook.Application")
    Set ws = ActiveSheet
    
    On Error GoTo MyHandler

    For Each cell In ws.Range("A2:A2000")

        Set objMail = objOutlook.CreateItem(0)

        With objMail
            .To = cell.Value
            .Cc = "email@email.com"
            .Subject = cell.Offset(0, 1).Value
            .Body = cell.Offset(0, 2).Value
            .Attachments.Add cell.Offset(0, 3).Value
            .Display
        End With

        Set objMail = Nothing
    Next cell

    Set ws = Nothing
    Set objOutlook = Nothing

MyHandler:
    MsgBox "Review email messages"

End Sub

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    你基本上需要对文件夹中的每个文件重复Attachment.Add方法:

    Sub SendMail()
    
    ActiveWorkbook.RefreshAll
    
    Dim objOutlook As Object
    Dim objMail As Object
    Dim ws As Worksheet
    Dim StrFile As String, StrPath As String
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set ws = ActiveSheet
    
    On Error GoTo MyHandler
    
    For Each cell In ws.Range("A2:A2000")
    
       Set objMail = objOutlook.CreateItem(0)
    
        With objMail
            .To = cell.Value
            .Cc = "email@email.com"
            .Subject = cell.Offset(0, 1).Value
            .Body = cell.Offset(0, 2).Value
        End With
    
            StrPath = "D:\any_folder\" 
            StrFile = Dir(StrPath & "*.*")
    
            Do While Len(StrFile) > 0
                objMail.Attachments.Add StrPath & StrFile
                StrFile = Dir
            Loop  
    
        objMail.Display
    
        Set objMail = Nothing
    Next cell
    
     Set ws = Nothing
     Set objOutlook = Nothing
    
    MyHandler:
      MsgBox "Review email messages"
    
    End Sub
    

    【讨论】:

    • 感谢您的帮助,但我收到编译错误:附件行中的引用无效或不合格。
    • 我刚刚更正了源代码。您需要指定父对象。
    • 它现在所做的是从根文件夹 C:\Temp\ 中获取所有文件。如何使其通过 D 列中指定的文件夹进行排序并从那里获取文件?我改变了 StrPath 如下。 StrPath = "C:\TEMP\"
    【解决方案2】:

    这会做你想做的。

    Sub Send_Files()
    'Working in Excel 2000-2016
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
        Dim OutApp As Object
        Dim OutMail As Object
        Dim sh As Worksheet
        Dim cell As Range
        Dim FileCell As Range
        Dim rng As Range
    
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set sh = Sheets("Sheet1")
    
        Set OutApp = CreateObject("Outlook.Application")
    
        For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    
            'Enter the path/file names in the C:Z column in each row
            Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    
            If cell.Value Like "?*@?*.?*" And _
               Application.WorksheetFunction.CountA(rng) > 0 Then
                Set OutMail = OutApp.CreateItem(0)
    
                With OutMail
                    .to = cell.Value
                    .Subject = "Testfile"
                    .Body = "Hi " & cell.Offset(0, -1).Value
    
                    For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                        If Trim(FileCell) <> "" Then
                            If Dir(FileCell.Value) <> "" Then
                                .Attachments.Add FileCell.Value
                            End If
                        End If
                    Next FileCell
    
                    .Send  'Or use .Display
                End With
    
                Set OutMail = Nothing
            End If
        Next cell
    
        Set OutApp = Nothing
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    

    注意:

    Make a list in Sheets("Sheet1") with :
    
    In column A : Names of the people
    In column B : E-mail addresses
    In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)
    

    https://www.rondebruin.nl/win/s1/outlook/amail6.htm

    【讨论】:

    • 谢谢,之前也发现了,但问题是我需要指定每个文件,如果你在批量工作,这会变得非常乏味。假设您正在处理 200 个不同的文件夹,其中一些文件夹中有 18 个文件,我需要输入每个路径和文件名。
    • 哇。考虑到这一点,我认为 Excel 不适合这种工作。根据您使用的任何逻辑,您必须执行一些非常高级的 VBA 编码才能正确设置所有文件和文件路径。您甚至会如何审核或协调这种工具?
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 2018-11-19
    • 2019-06-17
    • 1970-01-01
    • 2016-08-21
    • 2012-05-18
    相关资源
    最近更新 更多