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



The information in this article applies to:

  • Microsoft Outlook 2000
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q259370

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 2000 Object Library

Use the Links collection to add and retrieve associated contacts for task and journal items. The following code demonstrates how to do this:
Private Sub AddLinkedContactToTask()
       Dim spOutlook As Outlook.Application
       Dim spNameSpace As Outlook.NameSpace
       Dim spTasksFolder As Outlook.MAPIFolder
       Dim spTasks As Outlook.Items
       Dim spTask As Outlook.TaskItem
       Dim spContactsFolder as Outlook.MAPIFolder
       Dim spContacts As Outlook.Items
       Dim spContact As Outlook.ContactItem
       Dim spLinks As Outlook.Links
       Dim spLink As Outlook.Link

       On Error GoTo Handler
       Set spOutlook = CreateObject("Outlook.Application.9")
       Set spNameSpace = spOutlook.GetNamespace("MAPI")
       'Get tasks folder.
       Set spTasksFolder = spNameSpace.GetDefaultFolder(olFolderTasks)
       Set spTasks = spTasksFolder.Items
       'Get first task item.
       Set spTask = spTasks.GetFirst
       Set spLinks = spTask.Links
       'Add new linked contact.
       Set spContactsFolder = spNameSpace.GetDefaultFolder(olFolderContacts)
       Set spContacts = spContactsFolder.Items
       Set spContact = spContacts.GetFirst
       Set spLink = spLinks.Add(spContact)
       spTask.Save
       Debug.Print spTask.ContactNames
       Debug.Print spTask.Subject
       spTask.Close(olSave)
       'Loop through links collection.
       For Each spLink In spLinks
           'Check link type before getting item.
           If spLink.Type = olContactItem Then
               Set spContact = spLink.Item
               If Not spContact Is Nothing Then
                  Debug.Print spContact.FullName
               End If
               Set spContact = Nothing
           End If
           Set spLink = Nothing
       Next
   Done:
       Set spContact = Nothing
       Set spContacts = Nothing
       Set spContactsFolder = Nothing
       Set spLink = Nothing
       Set spLinks = Nothing
       Set spTask = Nothing
       Set spTasks = Nothing
       Set spTasksFolder = Nothing
       Set spNameSpace = Nothing
       Set spOutlook = Nothing
       Exit Sub
   Handler:
       Resume Done
   End Sub
				
Outlook 97 and Outlook 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 linked contacts display names.

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:

260999 How To OL98: 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 KB259370