Share via


Print Total Opportunities

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

C#

  private void PrintTotalOpportunitiesInMarketingCampaign()
        {
            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 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.Folder accounts = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];

            Outlook.ContactItem newAccount = (Outlook.ContactItem)accounts.Items.Add("IPM.Contact.BCM.Account");
            newAccount.FullName = "Wide World Importers";
            newAccount.FileAs = "WWImporters";
            newAccount.Save();

            string accountEntryID = newAccount.EntryID;

            Outlook.Folder opportunities = (Outlook.Folder)bcmRootFolder.Folders["Opportunities"];

            Outlook.TaskItem newOpportunity1 = (Outlook.TaskItem)opportunities.Items.Add("IPM.Task.BCM.Opportunity");
            newOpportunity1.Subject = "Sales Opp with Wide World Importers";

            if (newOpportunity1.UserProperties["Parent Entity EntryID"] == null)
            {
                userProp = newOpportunity1.UserProperties.Add("Parent Entity EntryID", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
                userProp.Value = accountEntryID;
            }

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

            newOpportunity1.Save();

            Outlook.TaskItem newOpportunity2 = (Outlook.TaskItem)opportunities.Items.Add("IPM.Task.BCM.Opportunity");
            newOpportunity2.Subject = "New Sales Opportunity with Sara";

            if (newOpportunity2.UserProperties["Parent Entity EntryID"] == null)
            {
                userProp = newOpportunity2.UserProperties.Add("Parent Entity EntryID", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
                userProp.Value = accountEntryID;
            }

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

            newOpportunity2.Save();

            int totalOpportunities = 0;
            Outlook.TaskItem existOpportunity;

            IEnumerator en = opportunities.Items.GetEnumerator();
            en.MoveNext();
            for (int index = 0; index < opportunities.Items.Count; index++)
            {
                if (index > 0)
                {
                    en.MoveNext();
                }
                existOpportunity = (Outlook.TaskItem)en.Current;

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

        }

VBA

  Sub PrintTotalOpportunitiesInMarketingCampaign()

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim bcmRootFolder As Outlook.Folder
    Dim olFolders As Outlook.Folders
    Dim bcmAccountsFldr As Outlook.Folder
    Dim bcmOppFldr As Outlook.Folder
    Dim bcmCampaignsFldr As Outlook.Folder

    Dim newMarketingCampaign As Outlook.TaskItem
    Dim newAcct As Outlook.ContactItem
    Dim newOpp1 As Outlook.TaskItem
    Dim newOpp2 As Outlook.TaskItem
    Dim existOpp As Outlook.TaskItem
    Dim index As Integer

    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 bcmOppFldr = bcmRootFolder.Folders("Opportunities")
    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 newAcct = bcmAccountsFldr.Items.Add("IPM.Contact.BCM.Account")
    newAcct.FullName = "Wide World Importers"
    newAcct.FileAs = "Wide World Importers"
    newAcct.Email1Address = "someone@example.com"
    newAcct.Save
    
     Set newOpp1 = bcmOppFldr.Items.Add("IPM.Task.BCM.Opportunity")
     newOpp1.Subject = "OpportunityX"
     
     If (newOpp1.UserProperties("Parent Entity EntryID") Is Nothing) Then
        Set userProp = newOpp1.UserProperties.Add("Parent Entity EntryID", olText, False, False)
        userProp.Value = newAcct.EntryID
     End If
     
     If (newOpp1.UserProperties("Referred Entry Id") Is Nothing) Then
        Set userProp = newOpp1.UserProperties.Add("Referred Entry Id", olText, False, False)
        userProp.Value = newMarketingCampaign.EntryID
     End If
     
     newOpp1.Save

     Set newOpp2 = bcmOppFldr.Items.Add("IPM.Task.BCM.Opportunity")
     newOpp2.Subject = "OpportunityY"
     
     If (newOpp2.UserProperties("Parent Entity EntryID") Is Nothing) Then
        Set userProp = newOpp2.UserProperties.Add("Parent Entity EntryID", olText, False, False)
        userProp.Value = newAcct.EntryID
     End If
     
     If (newOpp2.UserProperties("Referred Entry Id") Is Nothing) Then
        Set userProp = newOpp2.UserProperties.Add("Referred Entry Id", olText, False, False)
        userProp.Value = newMarketingCampaign.EntryID
     End If
     
     newOpp2.Save
    
    Dim TotalOpportunity As Integer
    
    For index = 1 To (bcmOppFldr.Items.count) Step 1
        
        Set existOpp = bcmOppFldr.Items(index)
        
        If existOpp.ItemProperties("Referred Entry Id").Value = newMarketingCampaign.EntryID Then
            TotalOpportunity = TotalOpportunity + 1
        End If
    Next
    
    Debug.Print "Total Opportunity: " & TotalOpportunity
    
    Set newOpp1 = Nothing
    Set newOpp2 = Nothing
    Set newAcct = Nothing
    Set newMarketingCampaign = Nothing
    Set bcmCampaignsFldr = Nothing
    Set bcmOppFldr = 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