Delete an Account

To remove an Account 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 Account object.

C#

  private void DeleteAccount()
{ 

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

   string strQuery = "[FileAs] = 'Wide World Importers'"; 

   Outlook.ContactItem accountItem = (Outlook.ContactItem)accountsFolder.Items.Find(strQuery);
   if (accountItem != null)
   { 
      accountItem.Delete();
   }
   else
   { 
      Console.WriteLine("Account not found");
   } 
} 


VBA

  Sub DeleteAccount()

   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 existAcct As Outlook.ContactItem

   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 existAcct = bcmAccountsFldr.Items.Find("[FileAs] = 'Wide World'")

   If Not TypeName(existAcct) = "Nothing" Then

      existAcct.Delete

   Else

      MsgBox ("No account found with Full Name Wide World Importers")

   End If

   Set existAcct = Nothing
   Set bcmAccountsFldr = Nothing
   Set olFolders = Nothing
   Set bcmRootFolder = Nothing
   Set objNS = Nothing
   Set olApp = Nothing

End Sub 


See Also

Create an Account | Select an Account | Edit an Account | 2007 Office System: Updated Developer Content