Share via


Select an Account

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

C#

  private void FindAccount()
{ 

   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)
   { 
      Console.WriteLine("Account found"); 
   }

   else
   {
      Console.WriteLine("Account not found"); 
   }
   
   Console.ReadLine(); 

}

VBA

  Sub SelectAccount()

   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

      MsgBox ("Account selected successfully")

   Else

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

   End If

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

End Sub 
 

See Also

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