简体   繁体   English

如何向列中的所有人发送一封电子邮件

[英]How to send a single email to all people in a column

I found macros to send an email to each person in a column.我找到了向列中的每个人发送电子邮件的宏。

它应该向 B 列中的所有人发送电子邮件。

Column B shows the names which have "Yes" in column C. I have added this condition in Power Query. B 列显示在 C 列中具有“是”的名称。我在 Power Query 中添加了此条件。

Sub Send_Row_Or_Rows_Attachment_1()
    'Working in 2000-2016
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Dim intHowManyRows As Integer

    With Application
        .ScreenUpdating = False
    End With
    intHowManyRows = Application.Range("B2").CurrentRegion.Rows.Count

    For r = 1 To intHowManyRows
        'Save, Mail, Close and Delete the file
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = ThisWorkbook.Sheets("Sheet3").Range("B1").Value
            ' Cells(r, 2).Value
            .Subject = Cells(r, 3).Value
            '.Attachments.Add FullName  -> If you want to add attachments
            .Body = "Hi there" & vbNewLine & vbNewLine & "How are you " & Cells(r, 2)
            .Display  'Or use Send
        End With
    Next r

    Set OutMail = Nothing
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Or:或者:

Sub Test2()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
      LCase(Cells(cell.Row, "C").Value) = "yes" Then
        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .To = ThisWorkbook.Sheets("Sheet3").Range("B1").Value
            .Subject = "Reminder"
            .Body = "Dear " & Cells(cell.Row, "A").Value _
                  & vbNewLine & vbNewLine & _
                    "Please contact us to discuss bringing " & _
                    "your account up to date"
            'You can add files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send  'Or use Display
        End With
        On Error GoTo 0
        Set OutMail = Nothing
    End If
Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

I want to generate a single Outlook mail with all persons in column B in the "To" and also attach a file.我想生成一个 Outlook 邮件,其中包含“收件人”中 B 列中的所有人,并附加一个文件。

I adjusted Ron's code.我调整了 Ron 的代码。 See my comments and adjust it to fit your needs.查看我的评论并对其进行调整以满足您的需求。

EDIT: As per niton's suggestion, remove the on error resume next and see what line causes the error.编辑:根据 niton 的建议,接下来删除 on error resume 并查看导致错误的行。

Option Explicit

Public Sub SendEmail()
    ' For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    ' Working in Office 2000-2016
    ' Adapted by Ricardo Diaz ricardodiaz.co
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sourceTable As ListObject
    Dim evalRow As ListRow

    Dim counter As Long
    Dim toArray() As Variant

    Application.ScreenUpdating = False

    Set OutApp = CreateObject("Outlook.Application")

    Set sourceTable = Range("Table1").ListObject ' -> Set the table's name

    On Error GoTo cleanup


    ' Loop through each table's rows
    For Each evalRow In sourceTable.ListRows

        If evalRow.Range.Cells(, 2).Value Like "?*@?*.?*" And LCase(evalRow.Range.Cells(, 3).Value) = "yes" Then
            ReDim Preserve toArray(counter)
            toArray(counter) = evalRow.Range.Cells(, 2).Value
            counter = counter + 1
        End If

    Next evalRow

    ' Setup the email
    Set OutMail = OutApp.CreateItem(0)


    With OutMail
        ' Add gathered recipients
        For counter = 0 To UBound(toArray)
            .Recipients.Add (toArray(counter))
        Next counter

        .Subject = "Reminder"

        .Body = "Dear All" _
                & vbNewLine & vbNewLine & _
                "Please contact us to discuss bringing " & _
                "your account up to date"

        'You can add files also like this
        .Attachments.Add ("C:\test.txt") ' -> Adjust this path

        .Send ' -> Or use Display
    End With

    Set OutMail = Nothing

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Let me know if it works.让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM