首页  编辑  

Outlook中自动添加vcf附件为联系人

Tags: /计算机文档/Office/   Date Created:
Outlook 中,如何自动把邮件附件中的.vcf添加为联系人,避免繁琐的Open,save,Close几个步骤的操作?
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_27510820.html#a37353090
在Outlook中,按 Alt + F11,插入一个Module,然后写入代码:
Sub AddContacts()
Dim mai As Object
   
   If TypeName(Application.ActiveWindow) = "Explorer" Then
       For Each mai In Application.ActiveExplorer.Selection
           If mai.Class = olMail Then processContact mai
       Next
   ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
       If mai.Class = olMail Then processContact Application.ActiveInspector.CurrentItem
   Else
       Exit Sub
   End If
End Sub

Sub processContact(mai As MailItem)
Dim att As Attachment
Dim obj As Object
Dim con As Object
Dim strFullName As String
Dim strID As String
Dim strFolderPath As String
Const intTempFolder As Integer = 2
   
   strFolderPath = CreateObject("Scripting.FileSystemObject").GetSpecialFolder(intTempFolder)
   For Each att In mai.Attachments
       strID = ""
       If LCase(Right(att.FileName, 4)) = ".msg" Then
           att.SaveAsFile (strFolderPath & "\" & att.FileName)
           Set obj = Application.CreateItemFromTemplate(strFolderPath & "\" & att.FileName)
           With obj
               .Save
               strID = .EntryID
               If .Class = olDistributionList Then
                   strFullName = .DLName
               ElseIf Class = olContact Then
                   strFullName = .FullName
               End If
               .Close olDiscard
           End With
           Kill (strFolderPath & "\" & att.FileName)
       ElseIf LCase(Right(att.FileName, 4)) = ".vcf" Then
           att.SaveAsFile (strFolderPath & "\" & att.FileName)
           Set obj = Application.GetNamespace("MAPI").OpenSharedItem(strFolderPath & "\" & att.FileName)
           With obj
               strID = .EntryID
               strFullName = .FullName
               .Close olSave
           End With
           Kill (strFolderPath & "\" & att.FileName)
       End If
       If strID <> "" Then
           For Each con In Application.Session.GetDefaultFolder(olFolderContacts).Items
               If con.Class = olDistributionList Then
                   If con.DLName = strFullName And con.EntryID <> strID Then
                       Application.Session.GetItemFromID(strID).Delete
                       Exit For
                   End If
               ElseIf con.Class = olContact Then
                   If con.FullName = strFullName And con.EntryID <> strID Then
                       Application.Session.GetItemFromID(strID).Delete
                       Exit For
                   End If
               End If
           Next
       End If
   Next
End Sub
最后,自定义工具栏或者快速启动栏,添加 一个按钮,按钮动作是运行宏AddContacts,当然你也可以添加一个规则收到带有附件的邮件的时候,自动处理。
重复处理上,会直接删除已有的然后重新导入。