2012-05-17 32 views
2

我有1000多個聯繫人,每個聯繫人都有一組常用職位。我希望以編程方式將每個職位組(例如所有與職位名稱'總經理'的聯繫人)添加到分配名單(例如'總經理')中。將聯繫人添加到Outlook發佈清單

+0

很簡單。你有什麼嘗試? –

+0

我正在從一本名爲「Outlook編程聖經」的書中嘗試一些例子,但沒有一個能夠完成我想要的,而且我沒有太多的運氣修改代碼。 – tonyyeb

+0

你想要什麼代碼? –

回答

6

好的,這裏只是默認聯繫人文件夾的一個例子。同樣,您必須前往DL可能存在的每個文件夾,從默認聯繫人文件夾開始,在創建Dist列表之前檢查它是否存在。

嘗試和測試(在Outlook VBA)

Option Explicit 

Sub GetJobList() 
    Dim olApp As Outlook.Application 
    Dim olNmspc As Outlook.NameSpace 
    Dim olAdLst As Outlook.AddressList 
    Dim olAdLstEntry As Outlook.AddressEntry 
    Dim olDLst As Outlook.DistListItem, olDLstItem As Outlook.DistListItem 
    Dim olMailItem As Outlook.MailItem 
    Dim olRecipients As Outlook.Recipients 

    Dim jobT() As String, JobTitle As String 
    Dim i As Long 

    Set olApp = New Outlook.Application 
    Set olNmspc = olApp.GetNamespace("MAPI") 

    i = 0 

    '~~> Loop through the address entries 
    For Each olAdLst In olNmspc.AddressLists 
     Select Case UCase(olAdLst.Name) 
      Case "CONTACTS" 
       '~~> Get the Job Title 
       For Each olAdLstEntry In olAdLst.AddressEntries 
        On Error Resume Next 
        JobTitle = Trim(olAdLstEntry.GetContact.JobTitle) 
        On Error GoTo 0 

        If JobTitle <> "" Then 
         ReDim Preserve jobT(i) 
         jobT(i) = olAdLstEntry.GetContact.JobTitle 
         i = i + 1 
        End If 
       Next 
     End Select 
    Next 

    '~~> Loop through the job title to create the distribution lists 
    For i = LBound(jobT) To UBound(jobT) 
     '~~> Check if the DL List exists 
     On Error Resume Next 
     Set olDLst = olNmspc.GetDefaultFolder(olFolderContacts).Items(jobT(i)) 
     On Error GoTo 0 

     '~~> If not then create it 
     If olDLst Is Nothing Then 
      Set olDLst = olApp.CreateItem(7) 
      olDLst.DLName = jobT(i) 
      olDLst.Save 
     End If 
    Next i 

    '~~> Loop through the address entries to add contact to relevant Distribution list 
    For Each olAdLst In olNmspc.AddressLists 
     Select Case UCase(olAdLst.Name) 
      Case "CONTACTS" 
       '~~> Get the Job Title 
       For Each olAdLstEntry In olAdLst.AddressEntries 
        On Error Resume Next 
        JobTitle = Trim(olAdLstEntry.GetContact.JobTitle) 
        On Error GoTo 0 

        If JobTitle <> "" Then 
         On Error Resume Next 
         Set olDLst = olNmspc.GetDefaultFolder(olFolderContacts).Items(JobTitle) 
         On Error GoTo 0 

         '~~> Create a mail item 
         Set olMailItem = olApp.CreateItem(0) 
         Set olRecipients = olMailItem.Recipients 
         olRecipients.Add olAdLstEntry.GetContact.Email1Address 

         '~~> Add to distribution list 
         With olDLst 
          .AddMembers olRecipients 
          .Close olSave 
         End With 

         Set olMailItem = Nothing 
         Set olRecipients = Nothing 
        End If 
       Next 
     End Select 
    Next 

    Set olNmspc = Nothing 
    Set olApp = Nothing 
    Set olDLst = Nothing 

End Sub 
+0

嗨。我得到一個錯誤91,對象變量或塊沒有設置在這一行:olRecipients.Add olAdLstEntry.GetContact.Email1Address - 我只有一個聯繫人和DList正確創建,並且聯繫人有一個email1地址。有任何想法嗎? – tonyyeb

+0

我認爲它可能會找到剛創建並將其視爲聯繫人的DList。研究檢查聯繫人類型的方法,以及是否爲DList來跳過它。 – tonyyeb

+0

好吧,我得到它的工作,但問題是聯繫人不添加到DList,只是他們的電子郵件地址。因此,當聯繫人更新爲新的電子郵件地址時,如果單擊「立即更新」,則此更改不會反映在DList中。 – tonyyeb