2015-07-10 44 views
1

人們經常轉發郵件給我,並要求我回復CC中的原始發件人。我認爲將這個人放在收件人字段和CC中的轉發人員是更加簡潔的。所以我喜歡交換它們。我想出了這塊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 

不幸的是,收件人被複製爲文本。因此,Outlook將在我們公司的地址簿中再次搜索它們。因爲它是一家大公司,有時會發現錯誤的人,甚至聲稱一個人是未知的。

我已經用objmail.Recipients進行了實驗,但我只有一些奇怪的錯誤。注意:To和CC字段中可能有多個人。

回答

0

您可以從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 
+0

爲什麼不恰當地改變Type屬性? –

+0

我試過'如果objRecipient.type = olTo然後objRecipient.type = olCC'但它不起作用。也許objMail.Recipients必須更新,或者To和CC文本框不顯示當前值。我沒有得到它的工作方式。通過生成一個字符串,它工作正常。 – Dorian

+0

>但它不起作用 - 你是什麼意思? –

0

中的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。