【问题标题】:outlook vba swap recipientsOutlook vba 交换收件人
【发布时间】:2015-07-10 09:47:21
【问题描述】:

人们经常将邮件转发给我,并要求我回复原发件人,他在 CC 中。我认为将这个人放在 To 字段中,将转发者放在 CC 中会更整洁。所以我喜欢交换它们。我想出了这段 VBA:

Sub Swap()
Dim objMail As Outlook.MailItem
Set objMail = Application.ActiveInspector.CurrentItem

With objMail
   a$ = .To
   .To = .CC
   .CC = a$
End With

Set objMail = Nothing
End Sub

很遗憾,收件人被复制为文本。因此 Outlook 将在我们公司的通讯录中再次搜索它们。而且由于是大公司,有时会找错人,甚至声称某人不认识。

我已经尝试过 objmail.Recipients,但我只遇到了一些奇怪的错误。注意:收件人和抄送字段中可能有多个人。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您可以从objMail.Recipients(x).Address 中获取电子邮件地址。以下代码在我的机器上运行良好:

    Public Sub Swap()
    Dim objOutlook As Outlook.Application ' i use this, because i'm working in MS Access
    Dim objMail As Outlook.MailItem
    Dim objRecipient As Outlook.recipient
    Dim strTo As String
    Dim strCC As String
    
    Set objOutlook = GetObject(, "Outlook.Application") ' i use this, because i'm working in MS Access
    Set objMail = objOutlook.ActiveInspector.CurrentItem
    
    For Each objRecipient In objMail.Recipients ' here we loop through all recipients
    
        If objRecipient.type = olTo Then ' check if the current recipient is in To section
            strCC = strCC & ";" & objRecipient.Address ' add it to the CC string
    
        ElseIf objRecipient.type = olCC Then ' check if the current recipient is in CC section
            strTo = strTo & ";" & objRecipient.Address 'add it to the To string
    
        End If
    Next objRecipient
    
    If strTo <> "" Then
        objMail.To = MID(strTo, 2) ' we cut off the leading semicolon of our string
    End If
    
    If strCC <> "" Then
        objMail.CC = MID(strCC, 2) ' same here
    End If
    
    Set objMail = Nothing
    End Sub
    

    【讨论】:

    • 为什么不适当地更改 Type 属性?
    • 我试过If objRecipient.type = olTo Then objRecipient.type = olCC,但没用。也许 objMail.Recipients 必须更新,或者 To 和 CC 文本框不显示当前值。我没有让它那样工作。通过生成一个字符串,它工作得很好。
    • 我打开了一封新电子邮件,在收件人字段中写入“testTo@test.com”,在 CC 字段中写入“testCC@test.com”并运行代码,但没有任何反应.地址没有交换,也没有错误消息。然后我用字符串试了一下,效果很好。
    • 我一直遇到同样的问题。我已经尝试过所有项目并使用 BCC 类型作为临时存储(我从不使用它)。但什么也没有发生。我想避免使用 Resolve。我必须说,Dorian 的解决方案使得找不到人或找错人的可能性不大。
    • 我已将此行添加到您的代码中:objMail.recipients.ResolveAll。尽管我打算交换已解决的项目,但我想这已经足够了。所以Dorian,我很感谢你的回答。我将使用您的解决方案。我的“声誉”还不够高,无法将您的答案标记为有用。但我认为这个问题已经解决了。
    【解决方案2】:

    MailItem 类的To 属性返回一个以分号分隔的字符串列表,其中显示Outlook 项目的收件人收件人的名称。此属性仅包含显示名称。相反,应该使用Recipients 集合来修改收件人。

    您只需要获取Recipients 集合的实例,请参阅 MailItem 类的相应属性,该属性返回 Outlook 项目的收件人对象集合。然后更改条目的Type 属性。

    根据收件人的类型,此属性返回或设置一个整数,该整数对应于以下常量之一的等效数字:

    • JournalItem 收件人:OlJournalRecipientType 常量 olAssociatedContact。
    • MailItem 收件人:以下 OlMailRecipientType 常量之一:olBCColCC、olOriginator 或 olTo
    • MeetingItem 收件人:以下 OlMeetingRecipientType 常量之一:olOptional、olOrganizer、olRequired 或 olResource。
    • TaskItem 收件人:以下任一 OlTask​​RecipientType 常量:olFinalStatus 或 olUpdate。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      相关资源
      最近更新 更多