Add Method (Links Collection) Example

Add Method (Links Collection) Example

This Microsoft Visual Basic/Visual Basic for Applications example creates a new task item, then prompts the user for the name of a contact to link to the item. If the contact is found, it is added to the item’s Links collection.

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myTask As Outlook.TaskItem
Dim myContact As Outlook.ContactItem
Set myTask = myOlApp.CreateItem(olTaskItem)
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
tempstr = InputBox("Enter the name of the contact to link to this task")
If tempstr <> "" Then
    tempstr = "[Full Name] = """ & tempstr & """"
    Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
    Set myContact = myItems.Find(tempstr)
    myTask.Links.Add myContact
myTask.Display

If you use VBScript, you do not create the Application object, and you cannot use named constants. This example shows how to perform the same task using VBScript.

Set myTask = Application.CreateItem(3)
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(10)
tempstr = InputBox("Enter the name of the contact to link to this task")
If tempstr <> "" Then
    tempstr = "[Full Name] = """ & tempstr & """"
    Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
    Set myContact = myItems.Find(tempstr)
    myTask.Links.Add myContact
myTask.Display