简体   繁体   English

Outlook VBA交换收件人

[英]outlook vba swap recipients

People often forward mails to me and ask me to reply to the original sender, who is in CC. 人们经常将邮件转发给我,并要求我回复抄送原始发送者。 I think it's more neat to put this person in the To field and the forwarder in CC. 我认为将这个人放在“收件人”字段中,并在CC中将转发器更整洁。 So I like to swap them. 所以我喜欢交换它们。 I came up with this piece of VBA: 我想到了这段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

Unfortunately, the recipients are copied as text. 不幸的是,收件人被复制为文本。 So Outlook will search for them again in our company's address book. 因此,Outlook将在我们公司的通讯簿中再次搜索它们。 And as it is a large company, sometimes it finds the wrong person, or even claims a person is unknown. 作为一家大公司,有时会找到错误的人,甚至声称一个人不认识。

I've experimented with objmail.Recipients but I only got some weird errors. 我已经尝试过objmail.Recipients,但只有一些奇怪的错误。 Note: There might be multiple people in both the To and the CC field. 注意:“收件人”和“抄送”字段中可能有多个人。

you can fetch the email address out of objMail.Recipients(x).Address . 您可以从objMail.Recipients(x).Address获取电子邮件地址。 The following code works fine on my machine: 以下代码在我的机器上可以正常运行:

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

The To property of the MailItem class returns a semicolon-delimited string list of display names for the To recipients for the Outlook item. MailItem类的To属性返回Outlook项目的To收件人的显示名称的分号分隔的字符串列表。 This property contains the display names only. 此属性仅包含显示名称。 Instead, the Recipients collection should be used to modify recipients. 而是,应使用“ 收件人”集合来修改收件人。

You just need to get an instance of the Recipients collection, see the corresponsing property of the MailItem class which returns a collection of Recipient objects for an Outlook item. 您只需要获取一个Recipients集合的实例,请参阅MailItem类的相应属性,该属性为Outlook项目返回一个Recipient对象的集合。 And then change the Type property of the entries. 然后更改条目的Type属性。

Depending on the type of recipient, this property returns or sets an integer corresponding to the numeric equivalent of one of the following constants: 根据收件人的类型,此属性返回或设置一个整数,该整数对应于以下常量之一的数值等效项:

  • JournalItem recipient: the OlJournalRecipientType constant olAssociatedContact. JournalItem收件人:OlJournalRecipientType常量olAssociatedContact。
  • MailItem recipient: one of the following OlMailRecipientType constants: olBCC , olCC , olOriginator, or olTo . MailItem收件人:以下OlMailRecipientType常量之一: olBCColCC ,olOriginator或olTo
  • MeetingItem recipient: one of the following OlMeetingRecipientType constants: olOptional, olOrganizer, olRequired, or olResource. MeetingItem收件人:以下OlMeetingRecipientType常量之一:olOptional,olOrganizer,olRequired或olResource。
  • TaskItem recipient: either of the following OlTaskRecipientType constants: olFinalStatus, or olUpdate. TaskItem收件人:以下OlTask​​RecipientType常量之一:olFinalStatus或olUpdate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM