Share via


Print Total Leads

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

C#

  private void PrintTotalLeadsInMarketingCampaign()
        {
            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["Lead"] == null)
            {
                userProp = newContact1.UserProperties.Add("Lead", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olYesNo, false, false);
                userProp.Value = true;
            }

            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["Lead"] == null)
            {
                userProp = newContact2.UserProperties.Add("Lead", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olYesNo, false, false);
                userProp.Value = true;
            }

            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();

            Outlook.ContactItem newContact3 = (Outlook.ContactItem)contacts.Items.Add("IPM.Contact.BCM.Contact");
            newContact3.FullName = "Simon Pearson";
            newContact3.FileAs = "Simon Pearson";

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

            newContact3.Save();

            int totalLeads = 0;
            Outlook.ContactItem existContact;
            bool leadValue;

            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.UserProperties["Lead"] == null)
                {
                    existContact.UserProperties.Add("Lead", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olYesNo, false, false);
                    leadValue = (bool)existContact.UserProperties["Lead"].Value;
                }
                else
                {
                    leadValue = (bool)existContact.UserProperties["Lead"].Value;
                }

                if (leadValue == true && existContact.ItemProperties["Referred Entry Id"].Value.Equals(taskItem.EntryID))
                {
                    totalLeads++;
                }
            }
            Console.WriteLine("TotalLeads is: {0}", totalLeads);
            Console.ReadLine();

        }

VBA

  Sub PrintTotalLeadsInMarketingCampaign()

    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 newContact3 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("Lead") Is Nothing) Then
        Set userProp = newContact1.UserProperties.Add("Lead", olYesNo, False, False)
        userProp.Value = True
     End If
     
     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("Lead") Is Nothing) Then
        Set userProp = newContact2.UserProperties.Add("Lead", olYesNo, False, False)
        userProp.Value = True
     End If
     
     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
     
     Set newContact3 = bcmContactsFldr.Items.Add("IPM.Contact.BCM.Contact")
     newContact3.FullName = "Rajesh Rotti "
     newContact3.FileAs = "Rajesh Rotti "
     newContact3.Email1Address = "someone@example.com"
     
     If (newContact3.UserProperties("Referred Entry Id") Is Nothing) Then
        Set userProp = newContact3.UserProperties.Add("Referred Entry Id", olText, False, False)
        userProp.Value = newMarketingCampaign.EntryID
     End If
     
     newContact3.Save
    
    Dim TotalLeads As Integer
    Dim leadValue As Boolean
    Dim count As Integer
    
    count = bcmContactsFldr.Items.count
    
    For index = 1 To count Step 1
        
        Set existContact = bcmContactsFldr.Items(index)
        
        If (existContact.UserProperties("Lead") Is Nothing) Then
            Set userProp = existContact.UserProperties.Add("Lead", olYesNo, False, False)
            leadValue = userProp.Value
        Else
            leadValue = existContact.UserProperties("Lead").Value
        End If
        If leadValue = True And existContact.ItemProperties("Referred Entry Id").Value = newMarketingCampaign.EntryID Then
            TotalLeads = TotalLeads + 1
        End If
    Next
    
    Debug.Print "Total Leads: " & TotalLeads
    
    Set newContact1 = Nothing
    Set newContact2 = Nothing
    Set newContact3 = 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 | 2007 Office System: Updated Developer Content