使用 VBA 发送消息时更改 Outlook 中的“Item.To”值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7697060/
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 "Item.To" value in outlook when sending a message using VBA
提问by Abdullah
I'm trying to change the email address in Send To field in Outlook when the user press send button. for example , if the current Item.To
value = '[email protected]'
it becomes '[email protected]'
.
当用户按下发送按钮时,我正在尝试更改 Outlook 中发送到字段中的电子邮件地址。例如,如果当前Item.To
值 ='[email protected]'
它变为'[email protected]'
.
I can change the subject , but failed with Item.To ( is it security issue ? ) :
我可以更改主题,但 Item.To 失败(是否存在安全问题?):
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Class <> olMail Then Exit Sub
Item.To = "[email protected]" ' Nope , It does not work
Item.Subject = "New Subject" ' It works
End Sub
Thanks
谢谢
回答by joeschwa
The MailItem.To
property is used only for display names. You probably want to use the Recipients collection as in this slightly modified example from Outlook's Help on the MailItem.Recipients
property:
该MailItem.To
属性仅用于显示名称。您可能希望使用 Recipients 集合,如该MailItem.Recipients
属性上 Outlook 帮助中稍加修改的示例所示:
Sub CreateStatusReportToBoss()
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("[email protected]")
myItem.Subject = "New Subject"
myItem.Display
End Sub
回答by Abdullah
I'm the question owner. I chose @joeschwa answer but also I want to display my code that cancel the current message and create new one ( you can change the recipients , message contents and anything else ) :
我是题主。我选择了@joeschwa 答案,但我也想显示取消当前消息并创建新消息的代码(您可以更改收件人、消息内容和其他任何内容):
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Class <> olMail Then Exit Sub
Dim newEm As String
Dim Rec As Recipient
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
myItem.Body = Item.Body
myItem.HTMLBody = Item.HTMLBody
myItem.Subject = Item.Subject & " RASEEL PLUGIN "
Cancel = True
For Each Rec In Item.Recipients
If InStr(1, Rec.AddressEntry, "@example.com", vbTextCompare) Then
newEm = "[email protected]"
Else
newEm = Rec.AddressEntry
End If
Set myRecipient = myItem.Recipients.Add(newEm)
myRecipient.Type = Rec.Type
Next
myItem.Send
End Sub
回答by Jan Guettler
It works for me. However, when changing recipient, it is also necessary first to delete the previous recipient. For example,
这个对我有用。但是,在更改收件人时,也需要先删除之前的收件人。例如,
x = .recipients.count
if x = 1 then .recipients(1).delete
.recipients.add "[email protected]"
x = .recipients.count
如果 x = 1 则 .recipients(1).delete
.recipients.add "[email protected]"