vba 更改 Outlook 邮件中的回复地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9259325/
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
Change reply address in Outlook mail
提问by Tony Dallimore
I have an Excel sheet with three columns Employee Name, Email ID and DOB.
我有一个 Excel 表,其中包含三列员工姓名、电子邮件 ID 和 DOB。
I wrote a macro which matches birth dates of the employees with today's date that will send an Outlook mail To the employee and Cc my department.
我写了一个宏,将员工的出生日期与今天的日期相匹配,该宏将向员工发送 Outlook 邮件并抄送我的部门。
When all the employees see that mail they can click on reply or reply to all.
当所有员工看到该邮件时,他们可以单击回复或回复所有人。
I wrote another Outlook macro which replaces To address field with his/her birthday person email id.
我写了另一个 Outlook 宏,它用他/她的生日人电子邮件 ID 替换了地址字段。
The second macro is working on my system on any Outlook email which is open.
第二个宏正在我的系统上处理任何打开的 Outlook 电子邮件。
Because I have the Outlook macro I am able to execute it but to perform same thing in all the employees systems they need this Outlook macro. How can I run it in their systems without putting this macro in their systems manually?
因为我有 Outlook 宏,所以我能够执行它,但在他们需要此 Outlook 宏的所有员工系统中执行相同的操作。我如何在他们的系统中运行它而不手动将这个宏放入他们的系统中?
回答by Tony Dallimore
The following code assumes ObjMail is the message you are creating.
以下代码假定 ObjMail 是您正在创建的消息。
' Delete any existing reply recipients
Do While ObjMail.ReplyRecipients.Count > 0
ObjMail.ReplyRecipients.Remove 1
Loop
' Add the new recipient
ObjMail.ReplyRecipients.Add "[email protected]"
' Send blind copy to other staff members
ObjMail.BCC = "Staff1.isp.com, Staff2.isp.com, Staff3.isp.com"
The message sent to staff will say it has come from whoever sends the birthday messages. But if anyone replies, the recipient will be "[email protected]".
发送给员工的消息会说它来自发送生日消息的人。但如果有人回复,收件人将是“[email protected]”。
I have sent blind copies to other staff members. This is not because the staff list is secret but because:
我已将密件发送给其他工作人员。这不是因为员工名单是秘密的,而是因为:
- If you have 500 staff members with an average 20 characters per address, use of CC would add 10,000 characters to each of 500 messages.
- It prevents staff using Reply All when adding their best wishes saving another 500 * 500 messages.
- Use ObjMail.CC if you would prefer to fill your company's server.
- 如果您有 500 名员工,每个地址平均有 20 个字符,则使用抄送会为 500 条消息中的每条增加 10,000 个字符。
- 它可以防止员工在添加他们最好的祝福时使用回复全部保存另外 500 * 500 条消息。
- 如果您希望填充公司的服务器,请使用 ObjMail.CC。
I worry about message size because many years ago I worked for the English NHS which had thousands of employees scattered across the country. Someone at a small hospital tried to advertise his bicycle for sale within the hospital but managed to advertise it to every employee in the country. I worked from home with a slow dial-up line; it took half-an-hour to download this message.
我担心消息的大小,因为多年前我在英国 NHS 工作,该机构有数千名员工分散在全国各地。一家小医院的某个人试图在医院内为他的自行车做广告,但设法向该国的每位员工做广告。我在家工作时拨号线路很慢;下载这条消息花了半个小时。
New section in response to request for full code of test routine
应要求提供测试程序完整代码的新部分
Below I include the full routine I used to test my answer. It was adapted from a routine I wrote for another answer. It creates an HTML body which you may not want but shows you how if you do. I have replaced the real email addresses I used for my tests with dummy addresses; otherwise it is unchanged.
下面我包括我用来测试我的答案的完整例程。它改编自我为另一个答案编写的例程。它会创建一个您可能不想要的 HTML 正文,但会向您展示如何操作。我已经用虚拟地址替换了我用于测试的真实电子邮件地址;否则不变。
Sub ReplyToRecipientWithBlindCopies()
' Create a mail item with a simple message.
' Send the mail item to "[email protected]" and make them
' the recipient of any replies.
' Send blind copies to all other recipients.
' Author: Tony Dallimore, York, England
Dim OlApp As Outlook.Application
Dim ObjMail As Outlook.MailItem
Dim MessageBody As String
' This creates a blue message on a grey background. This is a
' demonstration of what is possible; not a recommendation!
MessageBody = "<table width=""100%"" style=""Color:#0000FF;" & _
" background-color:#F0F0F0;""><tr><td align= ""center"">" & _
"Happy birthday from all your colleagues!</td></tr></table>"
Set OlApp = Outlook.Application
Set ObjMail = OlApp.CreateItem(olMailItem)
With ObjMail
.BodyFormat = olFormatHTML
.Subject = "Happy birthday!"
.HTMLBody = HeadAndBodyToHtmlDoc("", MessageBody)
' Remove any existing recipients
Do While .Recipients.Count > 0
.Recipients.Remove 1
Loop
' Remove any existing reply recipients
Do While .ReplyRecipients.Count > 0
.ReplyRecipients.Remove 1
Loop
' Add birthday person to Recipient and ReplyRecipient lists
.Recipients.Add "[email protected]"
.ReplyRecipients.Add "[email protected]"
' You will need to replace this with a loop
' to add all your staff members.
.BCC = "[email protected], [email protected], [email protected]"
' Display the prepared messages ready for any final changes.
' The user must send it.
.Display
End With
End Sub
Function HeadAndBodyToHtmlDoc(Head As String, Body As String) As String
' Wrap Head and Body created by caller in a standard envelope.
' Author: Tony Dallimore, York, England
HeadAndBodyToHtmlDoc = _
"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Frameset//EN""" & _
" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"">" & _
vbCr & vbLf & "<html xmlns=""http://www.w3.org/1999/xhtml""" & _
" xml:lang=""en"" lang=""en"">" & vbCr & vbLf & "<head><meta " & _
"http-equiv=""Content-Type"" content=""text/html; " & _
"charset=utf-8"" />" & vbCr & vbLf & Head & vbCr & vbLf & _
"</head><body>" & vbCr & vbLf & Body & "</body></html>"
End Function