How To OL98: Work with Linked Contacts of Outlook Items Using the Outlook Object Model - Visual Basic (260999)



The information in this article applies to:

  • Microsoft Outlook 98
  • Microsoft Office 97 for Windows
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q260999

SUMMARY

This article demonstrates how to work with linked contact items that are associated with task and journal items by using the Outlook Object Model. The process is enhanced in Outlook 2000 by the addition of the Links collection.

MORE INFORMATION

Outlook 97 or 98 Object Library

Use the Recipients collection of task and journal items to add new linked contacts.

Use the ContactNames property to retrieve a delimited list of the display names for the linked contacts. To obtain the actual contact item, you must iterate and manually search all contacts to find a match. The following code demonstrates how to do this:
Private Sub AddLinkedContactToTask()
    Dim spOutlook As Outlook.Application
    Dim spNameSpace As Outlook.NameSpace
    Dim spTasksFolder As MAPIFolder
    Dim spTasks As Items
    Dim spTask As TaskItem
    Dim spContactsFolder As MAPIFolder
    Dim spContacts As Items
    Dim spContact As ContactItem
    Dim spRecips As Recipients
    Dim spRecip As Recipient
    Dim s As String, sContact As String, s2 As String
    Dim i As Long, j As Long
    
    On Error GoTo Handler
    Set spOutlook = CreateObject("Outlook.Application")
    If spOutlook Is Nothing Then Exit Sub
    Set spNameSpace = spOutlook.GetNamespace("MAPI")
    'Get tasks folder.
    Set spTasksFolder = spNameSpace.GetDefaultFolder(olFolderTasks)
    Set spContactsFolder = spNameSpace.GetDefaultFolder(olFolderContacts)
    Set spTasks = spTasksFolder.Items
    'Get first taskitem.
    Set spTask = spTasks.GetFirst
    Debug.Print spTask.Subject
    'Add new linked contact.
    Set spRecips = spTask.Recipients
    Set spRecip = spRecips.Add("Contact Name")
    spRecip.Resolve
    spTask.Save
    'Loop through contact names.
    Set spContacts = spContactsFolder.Items
    ' Get a list of the contact names.
    s = spTask.ContactNames
    Debug.Print s
    If Len(s) > 0 Then
       i = 1: j = 1
       Do Until i = 0
           i = InStr(j, s, ";")
           If i > 0 Then
               sContact = Mid$(s, j, i - j)
               j = i + 1
           Else
               sContact = Mid$(s, j)
           End If
           s2 = "[FileAs] = " & Chr$(34) & sContact & Chr$(34)
           Set spContact = spContacts.Find(s2)
           If Not spContact Is Nothing Then
              Debug.Print spContact.FullName
           End If
           Set spContact = Nothing
       Loop
    End If
Done:
    Set spRecip = Nothing
    Set spRecips = Nothing
    Set spTask = Nothing
    Set spTasks = Nothing
    Set spTasksFolder = Nothing
    Set spContact = Nothing
    Set spContacts = Nothing
    Set spContactsFolder = Nothing
    Set spNameSpace = Nothing
    Set spOutlook = Nothing
    Exit Sub
Handler:
    Resume Done
End Sub
				

Outlook 2000 Object Library

Use the Links collection of task and journal items to add and retrieve linked contacts.

NOTE: When you use Outlook 2000, items that were created with Outlook 97 or Outlook 98 may contain valid data in the Recipients collection and ContactNames property; however, these properties do not contain valid data in items that are created with Outlook 2000.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

259370 How To OL2000: Work with Linked Contacts of Outlook Items Using the Outlook Object Model - Visual Basic


Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbMsg kbOutlookObj KB260999