Share via


Print Total Business Contacts

Outlook Developer Reference
Print Total Business Contacts

The following C# and Visual Basic for Applications (VBA) examples show how to use the Referred Entry Id property of Business Contacts to calculate the total Business Contacts that result from a Marketing Campaign.

  private void PrintTotalContactsInMarketingCampaign()
        {
            Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
            Outlook.Application olApp = (Outlook.Application)_app;
            Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
            Outlook.Folders folders = olNameSpace.Session.Folders;
            Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];
            Outlook.Folder contacts = (Outlook.Folder)bcmRootFolder.Folders["Business Contacts"];
            Outlook.Folder mktgCampFolder = (Outlook.Folder)bcmRootFolder.Folders["Marketing Campaigns"];
            Outlook.UserProperty userProp;
        Outlook.TaskItem taskItem = (Outlook.TaskItem)mktgCampFolder.Items.Add("IPM.Task.BCM.Campaign");
        taskItem.Subject = "New Marketing Campaign";
        if (taskItem.UserProperties["Campaign Code"] == null)
        {
            userProp = taskItem.UserProperties.Add("Campaign Code", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = "SP2";
        }

        if (taskItem.UserProperties["Campaign Type"] == null)
        {
            userProp = taskItem.UserProperties.Add("Campaign Type", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = "Direct Mail Print";
        }

        if (taskItem.UserProperties["Budgeted Cost"] == null)
        {
            userProp = taskItem.UserProperties.Add("Budgeted Cost", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olCurrency, false, false);
            userProp.Value = 243456;
        }

        if (taskItem.UserProperties["Delivery Method"] == null)
        {
            userProp = taskItem.UserProperties.Add("Delivery Method", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = "Word Mail Merge";
        }

        taskItem.StartDate = System.DateTime.Parse("4/20/2006");
        taskItem.DueDate = System.DateTime.Parse("5/20/2006");

        taskItem.Save();

        Outlook.ContactItem newContact1 = (Outlook.ContactItem)contacts.Items.Add("IPM.Contact.BCM.Contact");
        newContact1.FullName = "John Smith";
        newContact1.FileAs = "John Smith";

        if (newContact1.UserProperties["Referred Entry Id"] == null)
        {
            userProp = newContact1.UserProperties.Add("Referred Entry Id", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = taskItem.EntryID;
        }

        newContact1.Save();

        Outlook.ContactItem newContact2 = (Outlook.ContactItem)contacts.Items.Add("IPM.Contact.BCM.Contact");
        newContact2.FullName = "Liz Keyser";
        newContact2.FileAs = "Liz Keyser";

        if (newContact2.UserProperties["Referred Entry Id"] == null)
        {
            userProp = newContact2.UserProperties.Add("Referred Entry Id", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = taskItem.EntryID;
        }

        newContact2.Save();

        int totalContacts = 0;
        Outlook.ContactItem existContact;

        IEnumerator en = contacts.Items.GetEnumerator();
        en.MoveNext();
        for (int index = 0; index < contacts.Items.Count; index++)
        {
            if (index > 0)
            {
                en.MoveNext();
            }
            existContact = (Outlook.ContactItem)en.Current;

            if (existContact.ItemProperties["Referred Entry Id"].Value.Equals(taskItem.EntryID))
            {
                totalContacts++;
            }
        }
        Console.WriteLine("TotalContacts is: {0}", totalContacts);
        Console.ReadLine();

    }
  Sub PrintTotalContactsInMarketingCampaign()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim bcmRootFolder As Outlook.Folder
Dim olFolders As Outlook.Folders
Dim bcmCampaignsFldr As Outlook.Folder
Dim bcmContactsFldr As Outlook.Folder

Dim newMarketingCampaign As Outlook.TaskItem
Dim newContact1 As Outlook.ContactItem
Dim newContact2 As Outlook.ContactItem
Dim existContact As Outlook.ContactItem
Dim index As Integer
Dim userProp As Outlook.UserProperty

Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolders = objNS.Session.Folders

Set bcmRootFolder = olFolders("Business Contact Manager")
Set bcmContactsFldr = bcmRootFolder.Folders("Business Contacts")
Set bcmCampaignsFldr = bcmRootFolder.Folders("Marketing Campaigns")

Set newMarketingCampaign = bcmCampaignsFldr.Items.Add("IPM.Task.BCM.Campaign")
newMarketingCampaign.Subject = "Sales Project with Wide World Importers"
If (newMarketingCampaign.UserProperties("Campaign Code") Is Nothing) Then
    Set userProp = newMarketingCampaign.UserProperties.Add("Campaign Code", olText, False, False)
    userProp.Value = "SP2"
End If

If (newMarketingCampaign.UserProperties("Campaign Type") Is Nothing) Then
    Set userProp = newMarketingCampaign.UserProperties.Add("Campaign Type", olText, False, False)
    userProp.Value = "Mass Advertisement"
End If

If (newMarketingCampaign.UserProperties("Budgeted Cost") Is Nothing) Then
    Set userProp = newMarketingCampaign.UserProperties.Add("Budgeted Cost", olCurrency, False, False)
    userProp.Value = 243456
End If
newMarketingCampaign.Save

 Set newContact1 = bcmContactsFldr.Items.Add("IPM.Contact.BCM.Contact")
 newContact1.FullName = "John Smith"
 newContact1.FileAs = "John Smith"
 newContact1.Email1Address = "someone@example.com"
 
 If (newContact1.UserProperties("Referred Entry Id") Is Nothing) Then
    Set userProp = newContact1.UserProperties.Add("Referred Entry Id", olText, False, False)
    userProp.Value = newMarketingCampaign.EntryID
 End If

 newContact1.Save

 Set newContact2 = bcmContactsFldr.Items.Add("IPM.Contact.BCM.Contact")
 newContact2.FullName = "Nikolay Grachev"
 newContact2.FileAs = "Nikolay Grachev"
 newContact2.Email1Address = "someone@example.com"
 
 If (newContact2.UserProperties("Referred Entry Id") Is Nothing) Then
    Set userProp = newContact2.UserProperties.Add("Referred Entry Id", olText, False, False)
    userProp.Value = newMarketingCampaign.EntryID
 End If
 
 newContact2.Save

Dim TotalContact As Integer

For index = 1 To (bcmContactsFldr.Items.count) Step 1
    
    Set existContact = bcmContactsFldr.Items(index)
    
    If existContact.ItemProperties("Referred Entry Id").Value = newMarketingCampaign.EntryID Then
        TotalContact = TotalContact + 1
    End If
    
Next

Debug.Print "Total Contact: " & TotalContact

Set newContact1 = Nothing
Set newContact2 = Nothing
Set newMarketingCampaign = Nothing
Set bcmCampaignsFldr = Nothing
Set olFolders = Nothing
Set bcmRootFolder = Nothing
Set objNS = Nothing
Set olApp = Nothing

End Sub

See Also

Create a Marketing Campaign | Office Developer Center: Outlook 2007