2011-10-08 92 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]

相關問題