Share via


Print Total Accounts

Outlook Developer Reference
Print Total Accounts

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

  private void PrintTotalAccountsInMarketingCampaign()
        {
            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 accounts = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];
            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 newAccount1 = (Outlook.ContactItem)accounts.Items.Add("IPM.Contact.BCM.Account");
        newAccount1.FullName = "Wide World Importers";
        newAccount1.FileAs = "Wide World Importers";

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

        newAccount1.Save();

        Outlook.ContactItem newAccount2 = (Outlook.ContactItem)accounts.Items.Add("IPM.Contact.BCM.Account");
        newAccount2.FullName = "Software Services";
        newAccount2.FileAs = "Software Services";

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

        newAccount2.Save();

        int totalAccounts = 0;
        Outlook.ContactItem existAccount;

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

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

    }
  Sub PrintTotalAccountsInMarketingCampaign()
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 newMarketingCampaign As Outlook.TaskItem
Dim newAccount1 As Outlook.ContactItem
Dim newAccount2 As Outlook.ContactItem
Dim existAccount 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 bcmAccountsFldr = bcmRootFolder.Folders("Accounts")
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 newAccount1 = bcmAccountsFldr.Items.Add("IPM.Contact.BCM.Account")
 newAccount1.FullName = "CompanyX"
 newAccount1.FileAs = "CompanyX"
 newAccount1.Email1Address = "someone@example.com"
 
If (newAccount1.UserProperties("Referred Entry Id") Is Nothing) Then
    Set userProp = newAccount1.UserProperties.Add("Referred Entry Id", olText, False, False)
    userProp.Value = newMarketingCampaign.EntryID
End If

 newAccount1.Save

 Set newAccount2 = bcmAccountsFldr.Items.Add("IPM.Contact.BCM.Contact")
 newAccount2.FullName = "Wide World Importers"
 newAccount2.FileAs = "Wide World Importers"
 newAccount2.Email1Address = "someone@example.com"

If (newAccount2.UserProperties("Referred Entry Id") Is Nothing) Then
    Set userProp = newAccount2.UserProperties.Add("Referred Entry Id", olText, False, False)
    userProp.Value = newMarketingCampaign.EntryID
End If

 newAccount2.Save

Dim TotalAccount As Integer

For index = 0 To (bcmAccountsFldr.Items.Count) - 1
    If index = 0 Then
        Set existAccount = bcmAccountsFldr.Items.GetFirst
    Else
        Set existAccount = bcmAccountsFldr.Items.GetNext
    End If
    If existAccount.ItemProperties("Referred Entry Id").Value = newMarketingCampaign.EntryID Then
        TotalAccount = TotalAccount + 1
    End If
Next


Debug.Print "Total Account: " & TotalAccount

Set newAccount1 = Nothing
Set newAccount2 = Nothing
Set newMarketingCampaign = Nothing
Set bcmCampaignsFldr = Nothing
Set bcmAccountsFldr = 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