Delete an Opportunity

To remove an Opportunity object programmatically, first select the object and then call the Delete method. The following C# and Visual Basic for Applications (VBA) examples show how to delete a new Opportunity object.

C#

  private void DeleteOpportunity()
{ 

   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)
   { 
      taskItem.Delete(); 
   }
   
   else
   { 
      Console.WriteLine("Opportunity Not Found"); 
   } 

}


VBA

  Sub DeleteOpportunity() 

   Dim olApp As Outlook.Application
   Dim objNS As Outlook.NameSpace
   Dim olFolders As Outlook.Folders
   Dim bcmRootFolder As Outlook.Folder
   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 

      existOpportunity.Delete 

   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 | Edit an Opportunity | 2007 Office System: Updated Developer Content