使用 VBA 发送电子邮件到 Excel 上的电子邮件列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17172461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send E-Mail Using VBA To E-Mail Lists On Excel
提问by Anil Lulla
ve 90% of my VBA code written, just need to add the following. My macro pretty much runs and If Statement and if a certain condition applies, it will email it to a certain address. What I need it to do is to run the if statement, and if it meets the certain condition to email it to a list of 4-5 emails (maybe even more) which is in the same workbook but a different tab entitled "Email List".
我写了 90% 的 VBA 代码,只需要添加以下内容。我的宏几乎运行和 If Statement 如果某个条件适用,它将通过电子邮件将其发送到某个地址。我需要它做的是运行 if 语句,如果它满足特定条件,则将其通过电子邮件发送到 4-5 封电子邮件(甚至更多)的列表中,该列表位于同一个工作簿中,但名为“电子邮件列表”的不同选项卡”。
You can ignore the top part, this is what I'm currently working on.
你可以忽略上面的部分,这是我目前正在做的。
This is the updated code. Please advise as there are 8 sections so how will I transfer the Email code you came up with for the next 7? Thanks in advance man, really appreciate all your help.
这是更新后的代码。请告知,因为有 8 个部分,所以我将如何传输您为接下来的 7 个提供的电子邮件代码?在此先感谢人,非常感谢您的所有帮助。
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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.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 = "[email protected]" & "; [email protected]"
.Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
End Sub
回答by Ripster
Multiple emails can be sent by separating addresses with a semicolon.
可以通过用分号分隔地址来发送多封电子邮件。
Email "[email protected];[email protected]", Subject:=:Example Email", Body:="Example Mail"
You can search your sheet containing emails for the set of emails you need to send mail to, add each email to a string with a semicolon between each one.
您可以在包含电子邮件的工作表中搜索您需要向其发送邮件的一组电子邮件,将每封电子邮件添加到一个字符串中,每封电子邮件之间用分号隔开。
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
You can send emails using the following:
您可以使用以下方式发送电子邮件:
'---------------------------------------------------------------------------------------
' Desc : Sends an email
' Ex : Email SendTo:[email protected], 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
-Edit- This will get all of the emails
-Edit- 这将获得所有电子邮件
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