[英]Send email to multiple people based on common cell value using Excel VBA
I would like to add multiple recipients and attachments to one email message dictated by the value in a column.我想将多个收件人和附件添加到由列中的值指示的一封电子邮件。
I need to have people update their resumes, which will be attached, but I would like to group the emails by manager.我需要让人们更新他们的简历,将附上,但我想按经理对电子邮件进行分组。 The # of people under each manager ranges from 1-14.每个经理手下的人数从 1 到 14 不等。
The columns I have are:我拥有的列是:
B: Mgr Email address B: 经理电子邮件地址
C: Mgr last name C: 经理姓氏
D: Employee email D:员工邮箱
E: Emp first name E: Emp 名字
F: Emp last name F:Emp 姓氏
G: Status of resume G:简历状态
I created a macro that will loop through and create an email with proper attachment for each entry.我创建了一个宏,它将循环遍历并为每个条目创建一个带有适当附件的电子邮件。
I'd like to switch this to groups of employees by the value in column C or B. I have an inkling this will include arrays.我想通过 C 或 B 列中的值将其切换到员工组。我有一个暗示,这将包括数组。 I am a VBA novice.我是 VBA 新手。
What I have so far (with specific paths/emails renamed for privacy):到目前为止我所拥有的(为了隐私而重命名了特定路径/电子邮件):
Sub Test2()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim sSourcePath As String
Dim flpath As String
flpath = "C:\Resumes\"
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
'XYZ email address was hardcoded for testing purposes, but should also loop
If cell.Value = "XYZ@gmail.com" And _
Cells(cell.Row, "G").Value = "4. Need Update" _
Then
Set OutMail = OutApp.CreateItem(0)
sSourcePath = Dir(flpath & Cells(cell.Row, "E").Value & " *.docx")
On Error Resume Next
With OutMail
.To = cell.Value & ", " & Cells(cell.Row, "D").Value
'cced address is static
.CC = "ZZZ@gmail.com"
.Subject = "Resume needed"
.body = "Howdy!" _
& vbNewLine & vbNewLine & _
"Body text"
.attachments.Add flpath & sSourcePath
.Display '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
If I'm guessing correctly at what you want, here's a way that uses a collection of e-mails by manager.如果我猜对了您想要什么,这里有一种使用经理收集的电子邮件的方法。 They are created as you go, and then when you're finished, you can do whatever you want to the messages in the collection.它们随您创建,然后当您完成时,您可以对集合中的消息执行任何您想要的操作。
Dim allMessages as Collection
Dim currMessage as Object
Set allMessages = New Collection
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If Cells(cell.Row, "G").Value = "4. Need Update"
' Find the e-mail for the present manager
For Each currMessage in allMessages
If currMessage.CC = cell.Value Then
Exit For
End If
Next currMessage
' Create it, if it wasn't found
If currMessage Is Nothing Then
Set currMessage = OutApp.CreateItem(0)
allMessages.Add currMessage
With currMessage
.CC = cell.Value
.Subject = "Résumé Needed"
.Body = "Howdy!" & vbNewLine & vbNewLine & "Body text."
End With
End If
' Add the Message Recipient and Attachment
sSourcePath = Dir(flpath & Cells(cell.Row, "E").Value & " *.docx")
With currMessage
.To = .To & Iif(Len(.To) > 0,";","") & _
cell.Value & ", " & Cells(cell.Row, "D").Value
.Attachments.Add flpath & sSourcePath
End With
Set currMessage = Nothing
End If
Next cell
' Now do something with the messages.
For Each currMessage In allMessages
currMessage.Display
End If
Set currMessage = Nothing
Set allMessages = Nothing
Caveat: Given that I don't have your data and don't use Outlook presently, I have not tested the above code snippet.警告:鉴于我没有您的数据并且目前不使用 Outlook,我还没有测试上述代码片段。 The snippet primarily replaces your For...Next
Loop, with an additional loop and clean up at the end, some declarations at the beginning.该代码段主要用一个额外的循环替换你的For...Next
循环,并在最后进行清理,在开始时进行一些声明。 Let me know if it gives you problems, and I'll try to fix this answer based on what you tell me.如果它给您带来问题,请告诉我,我会根据您告诉我的内容尝试修复此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.