Share via


Edit an Opportunity

Outlook Developer Reference
Edit an Opportunity

To edit an Opportunity object programmatically, do the following:

  1. Select the object.
  2. Assign new values to those properties that you want to edit.
  3. Invoke the Save method on the object to save the changes.

The following C# and Visual Basic for Applications (VBA) example shows how to edit an Opportunity object.

  private void EditOpportunity()
        {
        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)
        {
            if (taskItem.UserProperties["Sales Stage"] == null)
            {
                Outlook.UserProperty userProp = taskItem.UserProperties.Add("Sales Stage", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
                userProp.Value = "Prospecting";
            }
            else
            {
                taskItem.UserProperties["Sales Stage"].Value = "Prospecting";
            }

            taskItem.Save();
        }

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

    }
  Sub UpdateOpportunity()

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

  If (existOpportunity.UserProperties("Sales Stage") Is Nothing) Then
    Set userProp = existOpportunity.UserProperties.Add("Sales Stage", olText, False, False)
    userProp.Value = "Prospecting"
  Else
    existOpportunity.ItemProperties("Sales Stage").Value = "Prospecting"
  End If

  existOpportunity.Save

Else

  MsgBox ("Failed to find the Opportuntiy with Subject Opportunity For John Smith to take over the Spring Sales")

End If

Set existOpportunity = Nothing Set bcmOppFolder = Nothing Set bcmRootFolder = Nothing Set olFolders = 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 | Select an Opportunity | Delete an Opportunity | Office Developer Center: Outlook 2007