|
|
A companion object to the Application object is the NameSpace object, which is retrieved by using the Application object's Session property. As noted earlier, some confusion can arise because functionality that you would expect to be on the Application object is actually often found on the NameSpace object. Further increasing the confusion is the Application.GetNameSpace method, which is an older way to get to a NameSpace object. This method takes a string for the type of NameSpace to return, implying that you can get different types of NameSpace objects. In reality, the GetNameSpace method accepts only one string ("MAPI"). In this chapter, we use the Application object's Session property (added in Outlook 98) to get a NameSpace object rather than the older GetNameSpace method. Working with the Root Folders of the Open Outlook StoresThe NameSpace object's Folders property returns a Folders collection, allowing you to iterate over all the root folders that are open within Outlook. Each root folder is the root of what is called a Store. A root folder could correspond to an Exchange account or some other e-mail account. It could also correspond to an Outlook data file, such as a .PST file. All folders and Outlook items under a particular root folder share a StoreID. You can iterate over the Folders collection using Visual Basic's For Each loop. You can also get to a particular MAPIFolder in the Folders collection using the index operator (). The index operator can be passed a String representing the name of the Folder in the Folders collection or a 1-based index representing the index of the Folder within the Folders collection. Although the Folders collection provides Add and Remove methods, these methods are not applicable to root folders, because root folders represent accounts that are added and removed by adding and removing e-mail accounts or adding and removing Outlook data files. The following section discusses how a Store is added and removed programmatically. Listing 11.10 illustrates iterating over the Folders collection using For Each. It also shows how to get a MAPIFolder using the index operator on the Folders collection. Finally, it shows how to add a new Folder to an existing store using the Folders collection's Add method. Listing 11.10. A VSTO Add-In That Iterates over the Root Folders and Adds a New Folder
Adding and Removing Outlook StoresTo add a Store programmatically, you can use the NameSpace object's AddStore or AddStoreEx method. The AddStore method takes a Store parameter of type Object. You can pass a String representing the complete filename of the PST file to add. If the PST file you provide does not exist, Outlook creates the file for you. AddStoreEx takes the same Store parameter of type Object that AddStore does. It also takes a second Type parameter of type OlStoreType. To this parameter, you can pass a member of the OlStoreType enumeration, which will control the format in which the PST file will be created should you pass a PST file that does not exist. The possible values you can pass are olStoreDefault, olStoreUnicode, and olStoreANSI. Use the NameSpace object's RemoveStore method to remove a Store programmatically. RemoveStore removes the Store from Outlook but does not delete the actual PST file or mailbox on the server associated with the Store. RemoveStore takes a Folder parameter of type MAPIFolder. This parameter must be one of the root folders in the NameSpace object's Folders collection. Determining the Current UserThe NameSpace object's CurrentUser property returns a Recipient object representing the logged-in user. Given a Recipient object, you can use the Recipient object's Name property to get the name of the logged-in user. Checking Whether Outlook Is OfflineYou can determine whether Outlook is offline by getting the value of the NameSpace object's Offline property. This property returns true if Outlook is offline and not connected to a server. Getting Standard Folders Such As the Inbox FolderA method already used in several examples in this chapter to get standard Outlook folders, such as the Inbox folder, is the NameSpace object's GetDefaultFolder method. This method takes a FolderType parameter of type OlDefaultFolders and returns a MAPIFolder object. Table 11.6 lists the members of the OlDefaultFolders enumeration that can be passed to GetDefaultFolder and the standard Outlook folder that is returned.
Getting a Folder or Outlook Item by IDAll Outlook items and folders are uniquely identified by an EntryID and a StoreID. All Outlook items and folders within a given Store share a StoreID. The EntryID is unique within a given Store. So the combination of an EntryID and StoreID uniquely identifies a folder or an Outlook item. When you have created a new Outlook item using the Items collection's Add method or the Application object's CreateItem method, the newly created Outlook item will not be assigned an EntryID until you call the Save method on the newly created item. Both a MAPIFolder and the 16 Outlook item types have an EntryID property that returns the EntryID for the folder or item as a String. But only MAPIFolders have a StoreID property. To determine the StoreID that corresponds to a particular Outlook item, you must get the parent MAPIFolder using the Parent property of an Outlook item and then determine the StoreID from the parent folder. The NameSpace object's GetFolderFromID method takes an EntryID parameter as a String and an optional StoreID parameter as an Object to which you can pass the StoreID as a String. If you omit the StoreID parameter by passing Type.Missing, Outlook assumes that it should look in the default Store (the Store in which the default Inbox and Calendar are located). The GetFolderFromID method returns the MAPIFolder object identified by the EntryID and StoreID. The NameSpace object's GetItemFromID method takes an EntryID parameter as a String and an optional StoreID parameter as an Object to which you can pass the StoreID as a String. If you omit the StoreID parameter, Outlook assumes that it should look in the default Store. The GetItemFromID method returns the Object for the Outlook item identified by the EntryID and StoreID. Then you can cast the returned Object to 1 of the 16 Outlook item types listed in Table 10.1 in Chapter 10, "Working with Outlook Events." Listing 11.11 illustrates getting a folder and an Outlook item by EntryID and StoreID. Listing 11.11. A VSTO Add-In That Uses the NameSpace Object's GetFolderFromID and GetItemFromID Methods
Accessing Address Books and Address EntriesThe NameSpace object's AddressLists property returns the AddressLists collection. The AddressLists collection is a collection containing all the available address books as AddressList objects. The AddressList object has an AddressEntries collection, which is a collection of AddressEntry objects. Each AddressEntry object represents an address in an address book. Listing 11.12 iterates over the available address books and displays the name of each address book. It also displays the name of the first address entry in each address book. Listing 11.12. A VSTO Add-In That Iterates over Available Address Books
Displaying the Outlook Select Folder Dialog BoxThe NameSpace object provides a method that allows you to display Outlook's Select Folder dialog box, shown in Figure 11.7. The Select Folder dialog box provides a way for the user to pick a folder, as well as create a new folder. The NameSpace object's PickFolder method displays the Select Folder dialog box as a modal dialog box. The method returns the MAPIFolder object corresponding to the folder the user picked in the dialog box. If the user cancels the dialog box, this method will return Nothing. Figure 11.7. Outlook's Select Folder dialog box.
|
|
|