Links Property Example

This Microsoft Visual Basic/Visual Basic for Applications example steps through the items in the Tasks folder and, if a task is not complete, displays the number of contacts linked to the item.

Dim myOlApp As New Outlook.Application
Dim myNSpace As Outlook.NameSpace
Dim myItems As Outlook.Items
Dim myItem As Outlook.TaskItem
Dim myLinks As Outlook.Links
Dim myLink As Outlook.Link
Set myNSpace = myOlApp.GetNamespace("MAPI")
Set myItems = myNSpace.GetDefaultFolder(olFolderTasks).Items
For x = 1 To myItems.Count
    If TypeName(myItems.Item(x)) = "TaskItem" Then
        Set myItem = myItems.Item(x)
        Set myLinks = myItem.Links
        Msg = myItem.Subject & " has " & myLinks.Count & " links."
        If myItem.Complete = False Then
            If MsgBox(Msg, vbOKCancel) = vbCancel Then Exit For
        End If
    End If
 Next x

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 myNSpace = Application.GetNamespace("MAPI")
Set myItems = myNSpace.GetDefaultFolder(13).Items
For x = 1 To myItems.Count
    If TypeName(myItems.Item(x)) = "TaskItem" Then
        Set myItem = myItems.Item(x)
        Set myLinks = myItem.Links
        Msg = myItem.Subject & " has " & myLinks.Count & " links."
        If myItem.Complete = False Then
            If MsgBox(Msg, 1) = 2 Then Exit For
        End If
    End If
 Next