2011-10-08 43 views
4

我试图在用户按发送按钮时在Outlook中的发送到字段中更改电子邮件地址。例如,如果当前的Item.To值= '[email protected]'它将变为'[email protected]'当使用VBA发送消息时,在Outlook中更改“Item.To”值

我可能会改变话题,但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 

感谢

回答

5

MailItem.To属性仅用于显示名称。你可能想在此略作修改例如在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 
+0

谢谢,我选择了这个答案,因为我不知道收件人没有。这个信息导致解决整个问题。谢谢 – Abdullah

4

我的问题所有者。我选择@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 
0

这对我的作品。但是,在更改收件人时,还必须先删除以前的收件人。例如,

x = .recipients.count 如果x = 1,则.recipients(1).delete
.recipients.add “[email protected]

相关问题