Select an Opportunity

To select an Opportunity object, use the Collection.Find method to select an item from the MAPI Folder. The Find method accepts as input a String query and filters items based on the query. If there are more than one items matching the query, only the first match is returned.

The following C# and Visual Basic for Applications (VBA) examples show how to select an Opportunity object.

C#

  private void SelectOpportunity()
        {

            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 oppFolder = (Outlook.Folder)bcmRootFolder.Folders["Opportunities"];

            string strQuery = "[Subject] = 'Sales Opp with Wide World Importers'";

            Outlook.TaskItem taskItem = (Outlook.TaskItem)oppFolder.Items.Find(strQuery);
            if (taskItem != null)
            {
                Console.WriteLine("Opportunity Found");
            }

            else
            {
                Console.WriteLine("Opportunity Not Found");
            }

            Console.ReadLine();

        } 

VBA

  Sub SelectOpportunity() 

   Dim olApp As Outlook.Application
   Dim objNS As Outlook.NameSpace
   Dim bcmRootFolder As Outlook.Folder
   Dim olFolders As Outlook.Folders
   Dim bcmOppFolder As Outlook.Folder
   Dim existOpportunity As Outlook.TaskItem 

   Set olApp = CreateObject("Outlook.Application")
   Set objNS = olApp.GetNamespace("MAPI")
   Set olFolders = objNS.Session.Folders
   Set bcmRootFolder = olFolders("Business Contact Manager")
   Set bcmOppFolder = bcmRootFolder.Folders("Opportunities") 

   Set existOpportunity = bcmOppFolder.Items.Find("[Subject] = 'Opportunity for Wide World Importers to enter into Retail Field'") 

   If Not TypeName(existOpportunity) = "Nothing" Then 

      MsgBox ("Opportunity Found") 

   Else 

      MsgBox ("Failed to find the Opportunity with Subject Opportunity for Wide World Importers to take over the Spring Sales") 

   End If 

   Set existOpportunity = Nothing
   Set bcmOppFolder = Nothing
   Set bcmRootFolder = Nothing
   Set olFolders = Nothing
   Set bcmRootFolder = Nothing
   Set objNS = Nothing
   Set olApp = Nothing 

End Sub


See Also

Create an Opportunity linked to an Account | Create an Opportunity linked to a Business Contact | Edit an Opportunity | Delete an Opportunity | 2007 Office System: Updated Developer Content