首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用VBA向Excel上的电子邮件列表发送电子邮件

使用VBA向Excel上的电子邮件列表发送电子邮件
EN

Stack Overflow用户
提问于 2013-06-18 15:09:12
回答 1查看 7.9K关注 0票数 0

VBA编写的VBA代码的90%,只需添加以下内容。我的宏基本上运行,如果语句和某个条件适用,它将电子邮件到一个特定的地址。我需要它做的是运行if语句,如果它满足一定的条件,将其发送到一个包含4-5封电子邮件(可能甚至更多)的列表中,该列表位于同一个工作簿中,但有一个不同的选项卡,名为“电子邮件列表”。

你可以忽略最上面的部分,这是我目前正在做的。

这是更新的代码。请告知,因为有8个部分,所以我将如何转移您提出的电子邮件代码为下一个7?谢谢,先生,真的很感谢你的帮助。

代码语言:javascript
运行
复制
Sub Send_Range()
    Dim row As Long
    Dim col As Long
    Dim rCell As Range
    Dim SendTo As String
    Dim i As Long

    row = Sheets("Email List").UsedRange.Rows.Count
    col = Sheets("Email List").UsedRange.Columns.Count

    If Not IsEmpty(Range("B4")) Then
        With Sheets("Email List")
            For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
                If rCell.Value <> "" Then
                    For i = 3 To row
                        If .Cells(i, rCell.Column).Value <> "" Then
                            SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
                        End If
                    Next
                End If
            Next
        End With
    End If

   If IsEmpty(Range("B4")) Then
   Else
      ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown)).Select
      ActiveWorkbook.EnvelopeVisible = True
   With ActiveSheet.MailEnvelope

      .Item.To = SendTo
      .Item.Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

       row = Sheets("Email List").UsedRange.Rows.Count
    col = Sheets("Email List").UsedRange.Columns.Count

    If Not IsEmpty(Range("B4")) Then
        With Sheets("Email List")
            For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
                If rCell.Value <> "" Then
                    For i = 3 To row
                        If .Cells(i, rCell.Column).Value <> "" Then
                            SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
                        End If
                    Next
                End If
            Next
        End With
    End If

   If IsEmpty(Range("H4")) Then
   Else
        ActiveSheet.Range("G3", ActiveSheet.Range("K3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - BNP" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

      If IsEmpty(Range("N4")) Then
   Else
        ActiveSheet.Range("M3", ActiveSheet.Range("Q3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - CITINY" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

   If IsEmpty(Range("T4")) Then
   Else
        ActiveSheet.Range("S3", ActiveSheet.Range("W3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - CSFB" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

      If IsEmpty(Range("Z4")) Then
   Else
        ActiveSheet.Range("Y3", ActiveSheet.Range("AC3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - DB" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

      If IsEmpty(Range("AF4")) Then
   Else
        ActiveSheet.Range("AE3", ActiveSheet.Range("AI3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - JPM" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

      If IsEmpty(Range("AL4")) Then
   Else
        ActiveSheet.Range("AK3", ActiveSheet.Range("AO3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - MS" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

      If IsEmpty(Range("AR4")) Then
   Else
        ActiveSheet.Range("AQ3", ActiveSheet.Range("AU3").End(xlDown)).Select

    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
      .Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
End Sub
EN

回答 1

Stack Overflow用户

发布于 2013-06-18 15:41:52

通过用分号分隔地址可以发送多封电子邮件。

代码语言:javascript
运行
复制
Email "email@example.com;email2@example.com", Subject:=:Example Email", Body:="Example Mail"

您可以在包含电子邮件的工作表中搜索需要发送邮件的电子邮件集,将每封电子邮件添加到在每个邮件之间带有分号的字符串中。

代码语言:javascript
运行
复制
Sub Example()
    Dim rCell As Range
    Dim SendTo As String
    Dim i As Long

    For Each rCell In Range(Cells(1, 1), Cells(1, ActiveSheet.UsedRange.Columns.Count))
        If rCell.Value = "DNP" Then
            For i = 3 To ActiveSheet.UsedRange.Rows.Count
                If Cells(i, rCell.Column).Value <> "" Then
                    SendTo = SendTo & Cells(i, rCell.Column + 1).Value & ";"
                End If
            Next
            Exit For
        End If
    Next

    Email SendTo
End Sub

您可以使用以下方式发送电子邮件:

代码语言:javascript
运行
复制
'---------------------------------------------------------------------------------------
' Desc  : Sends an email
' Ex    : Email SendTo:=email@example.com, Subject:="example email", Body:="Email Body"
'---------------------------------------------------------------------------------------
Sub Email(SendTo As String, Optional CC As String, Optional BCC As String, Optional Subject As String, Optional Body As String, Optional Attachment As Variant)
    Dim s As Variant              'Attachment string if array is passed
    Dim Mail_Object As Variant    'Outlook application object
    Dim Mail_Single As Variant    'Email object

    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)

    With Mail_Single
        'Add attachments
        Select Case TypeName(Attachment)
            Case "Variant()"
                For Each s In Attachment
                    If s <> Empty Then
                        If FileExists(s) = True Then
                            Mail_Single.attachments.Add s
                        End If
                    End If
                Next
            Case "String"
                If Attachment <> Empty Then
                    If FileExists(Attachment) = True Then
                        Mail_Single.attachments.Add Attachment
                    End If
                End If
        End Select

        'Setup email
        .Subject = Subject
        .To = SendTo
        .CC = CC
        .BCC = BCC
        .HTMLbody = Body
        On Error GoTo SEND_FAILED
        .Send
        On Error GoTo 0
    End With

    Exit Sub

SEND_FAILED:
    With Mail_Single
        MsgBox "Mail to '" & .To & "' could not be sent."
        .Delete
    End With
    Resume Next
End Sub

Function FileExists(ByVal Path As String) As Boolean
    'Remove trailing backslash
    If InStr(Len(Path), Path, "\") > 0 Then Path = Left(Path, Len(Path) - 1)
    'Check to see if the directory exists and return true/false
    If Dir(Path, vbDirectory) <> "" Then FileExists = True
End Function

-编辑-这会收到所有的电子邮件

代码语言:javascript
运行
复制
Sub Send_Range()
    Dim row As Long
    Dim col As Long
    Dim rCell As Range
    Dim SendTo As String
    Dim i As Long

    row = Sheets("Email List").UsedRange.Rows.Count
    col = Sheets("Email List").UsedRange.Columns.Count

    If Not IsEmpty(Range("B4")) Then
        With Sheets("Email List")
            For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
                If rCell.Value <> "" Then
                    For i = 3 To row
                        If .Cells(i, rCell.Column).Value <> "" Then
                            SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
                        End If
                    Next
                End If
            Next
        End With
    End If

    ActiveWorkbook.EnvelopeVisible = True

    With ActiveSheet.MailEnvelope
        SendTo = Left(SendTo, Len(SendTo) - 1)
        .Item.To = SendTo
        .Item.Subject = "Allocations - Barclays" & Format(Date, " mm/dd/yyyy")
        .Item.Send
    End With
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17172461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档