[英]Send Emails using Excel VBA from a table
I have a list of emails that are in a table and I would like to send these people emails.我有一个表格中的电子邮件列表,我想向这些人发送电子邮件。
At the moment, my code only references one cell where I have stored multiple emails.目前,我的代码只引用了一个存储多封电子邮件的单元格。
emailItem.To = Range("A2").Value
emailItem.CC = Range("B2").Value
How do I reference a table array so when I add or remove someone from the distribution list, it becomes 'dynamic'.如何引用表数组,以便当我从分发列表中添加或删除某人时,它变为“动态”。
Here is what my table looks like:这是我的桌子的样子:
Here is the code I'm working with:这是我正在使用的代码:
Option Explicit
Sub Send_Email_With_Attachment()
Dim emailApplication As Object
Dim emailItem As Object
Set emailApplication = CreateObject("Outlook.Application")
Set emailItem = emailApplication.CreateItem(0)
'Date Update in Subject Line
Dim lastSunday As Date
lastSunday = DateAdd("d", 1 - Weekday(Now), Now)
'Now build the email.
emailItem.To = Range("A2").Value
emailItem.CC = Range("B2").Value
emailItem.Subject = "Training Report - " & Format(lastSunday, "dd-MM-yyyy")
emailItem.Body = "Dear All" & vbCrLf & vbCrLf & "Please find attached the Weekly Training report." & vbCrLf & vbCrLf & "Kind Regards,"
' Attach any file from computer
'Send the email
emailItem.Display
End Sub
Instead of代替
emailItem.To = Range("A2").Value
emailItem.CC = Range("B2").Value
use利用
Dim EmailTable As ListObject ' define your email table in the sheet
Set EmailTable = ThisWorkbook.Worksheets("Sheet1").ListObjects("Email2")
emailItem.To = Join(Application.Transpose(EmailTable.ListColumns("To").DataBodyRange.Value2), ";")
emailItem.CC = Join(Application.Transpose(EmailTable.ListColumns("CC").DataBodyRange.Value2), ";")
Note that Email2
is the name of your table and Sheet1
needs to be the name of the worksheet.请注意,
Email2
是您的表的名称,而Sheet1
需要是工作表的名称。 Transpose
will make a one dimensional array out of the column that we can Join
into a string seperated by ;
Transpose
将从列中生成一个一维数组,我们可以将其Join
到一个由;
分隔的字符串中。
This is code I used to generate emails.这是我用来生成电子邮件的代码。
The code will only work if BOTH 'To & CC' are in the same Table.该代码仅在“收件人和抄送”都在同一个表中时才有效。
The code will work only on the ACTIVE Sheet that you're in.该代码仅适用于您所在的活动表。
The code also references a PAST date and can be modified very easily.该代码还引用了过去的日期,可以很容易地修改。 Change this part of the code: "("d", 1 - Weekday(Now), Now)" to suit your need.
更改这部分代码:"("d", 1 - Weekday(Now), Now)" 以满足您的需要。
Option Explicit
Sub Send_Email_With_Attachment() Dim OutApp As Object, OutMail As Object Dim emailTo As String, emailCC As String Dim lastSunday As Date Dim c As Range Sub Send_Email_With_Attachment() 将 OutApp 调暗为 Object,OutMail 作为 Object 将 emailTo 作为字符串调暗,emailCC 作为字符串调暗 lastSunday 作为日期调暗 Z4A8A08F09D37B73B5333566
lastSunday = DateAdd("d", 1 - Weekday(Now), Now)
emailTo = WorksheetFunction.TextJoin(";", True, ActiveSheet.Range("Email2[To]"))
emailCC = WorksheetFunction.TextJoin(";", True, ActiveSheet.Range("Email2[CC]"))
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = emailTo
.CC = emailCC
.Subject = "Report - " & Format(lastSunday, "dd-MM-yyyy")
.Body = "Dear All" & vbCrLf & vbCrLf & _
"Please find attached the Weekly report." & vbCrLf & vbCrLf & "Kind Regards,"
'.Attachments.Add ""
.Send
End With
End Sub结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.