【问题标题】:Change "Item.To" value in outlook when sending a message using VBA使用 VBA 发送消息时更改 Outlook 中的“Item.To”值
【发布时间】:2011-10-08 13:16:05
【问题描述】:

当用户按下发送按钮时,我正在尝试更改 Outlook 中发送至字段中的电子邮件地址。例如,如果当前的 Item.To 值 = 'aaa@example.com' 则变为 'bbb@example.com'

我可以更改主题,但使用 Item.To 失败(是安全问题吗?):

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

   If Item.Class <> olMail Then Exit Sub

   Item.To = "bbb@example.com"  ' Nope , It does not work
   Item.Subject = "New Subject" ' It works

End Sub

谢谢

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    MailItem.To 属性仅用于显示名称。您可能希望在 Outlook 帮助中 MailItem.Recipients 属性的这个稍作修改的示例中使用收件人集合:

    Sub CreateStatusReportToBoss()
    
     Dim myItem As Outlook.MailItem
     Dim myRecipient As Outlook.Recipient
    
     Set myItem = Application.CreateItem(olMailItem)
     Set myRecipient = myItem.Recipients.Add("bbb@example.com")
     myItem.Subject = "New Subject"
     myItem.Display
    
    End Sub
    

    【讨论】:

    • 谢谢,我选择了这个答案,因为我不了解收件人。此信息导致解决整个问题。谢谢
    【解决方案2】:

    我是问题所有者。我选择了@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 = "example@example.net"
        Else
            newEm = Rec.AddressEntry
       End If
    
        Set myRecipient = myItem.Recipients.Add(newEm)
        myRecipient.Type = Rec.Type
    
       Next
    
       myItem.Send
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      它对我有用。但是,更改收件人时,也需要先删除之前的收件人。例如,

      x = .recipients.count 如果 x = 1 则 .recipients(1).delete
      .recipients.add "abc@dfg.com"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-23
        • 1970-01-01
        • 1970-01-01
        • 2018-05-04
        • 2023-03-20
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        相关资源
        最近更新 更多