Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
The Explorer object represents an Outlook Explorer windowthe main window in Outlook that displays views of folders. It is possible to open multiple Explorer windows; you can right-click a folder in one Explorer window and choose the option Open in New Window. Doing so creates a new Explorer window with the folder you selected to open in a new window as the active folder. Working with the Selected Folder, View, and Items
The Explorer object has several methods and properties that enable you to work with the selected folder in the Explorer window, the view being used to display the list of items in that folder, and the selected items. The CurrentFolder property returns a MAPIFolder object representing the folder selected in the Explorer window. An Explorer window always has a selected folder. To change the selected folder in an Explorer window, you can use the Explorer object's SelectFolder method, which takes as a parameter the MAPIFolder object you want to select. You can also determine whether a particular folder is selected by using the Explorer object's IsFolderSelected method, which takes as a parameter the MAPIFolder object you want to check to see whether it is selected. The IsFolderSelected method returns TRue if the folder is selected in the Explorer window and False if it is not. Listing 11.5 shows some code that displays the name of the selected folder. Then it checks to see whether the Contacts folder is selected. If that folder isn't selected, the code selects it. Finally, it displays the name of the newly selected folder. Listing 11.5 uses the NameSpace object's GetDefaultFolder method to get a MAPIFolder object for the Contacts folder. Listing 11.5. A VSTO Add-In That Selects the Contacts Folder
The CurrentView property returns a View object representing the view that is being used to display the items in the folder. A folder has a number of views that can be used to display its contents, such as view by date, by conversation, by sender, and so on. It is also possible to define custom views. You can see the views that are defined for a given folder by selecting that folder in an Explorer window and then choosing View > Arrange By > Current View > Define Views to display the dialog box shown in Figure 11.4. Figure 11.4. The Custom View Organizer dialog box shows views associated with a folder.
You can change the view used by an Explorer window by setting the Explorer object's CurrentView property to a View object associated with the folder. Listing 11.6 demonstrates this by selecting the Inbox folder and then setting the view for the Inbox folder to one of the View objects associated with the folder. Listing 11.6. A VSTO Add-In That Selects the Inbox Folder and Changes the View
In addition to a selected folder and selected view, Outlook items can be selected in an Explorer window. A user can select multiple items in a folder by Shift-clicking to select a range of items or holding down the Ctrl key while clicking to select discontiguous items. To retrieve the items that are selected in an Explorer window, use the Explorer object's Selection property. The Selection property returns a Selection collection. The Selection collection has a Count property that gives you the number of selected Outlook items. The collection also has an Item method that allows you to get to an individual Outlook item that was selected, or you can use the For Each loop to iterate over a Selection collection and get back Outlook items that are selected. Outlook items are returned as type Object because they could be any of the 16 types of Outlook items (MailItem, ContactItem, and so on). In Listing 11.7, we handle the Application object's BeforeFolderSwitch event to display the items selected in a given folder before Outlook switches to a new folder. We use the late-bound Subject property to get the subject from each selected Outlook item. We know that the Subject property exists on all 16 types of Outlook items, so this is a safe property to get for any Outlook item contained in the selection. This simplifies the code so it does not have to have a cast to all 16 Outlook item types before accessing the Subject property. Listing 11.7. A VSTO Add-In That Iterates over the Selected Outlook Items in a Folder
Working with an Explorer Window
Table 11.4 lists several properties and methods used to set and get the position of an Explorer window, as well as some other commonly used properties and methods related to the management of the window.
Adding Buttons and Menus to an Explorer Window
The CommandBars property returns a CommandBars object, which is defined in the Microsoft Office 11.0 Object Library primary interop assembly (PIA) object. Outlook uses the same object model used by Word and Excel to work with buttons and menus in an Explorer window. Refer to Chapter 4, "Working with Excel Events," for more information on the CommandBars object hierarchy and examples of using the CommandBar objects. Listing 11.8 shows a VSTO add-in that creates a toolbar and a button, and handles the click event for the new button. Listing 11.8. A VSTO Add-In That Adds a Toolbar and Button to an Explorer Window
Associating a Web View with a Folder
It is possible to associate with an Outlook folder an HTML Web page by right-clicking a folder, choosing Properties, and then clicking the Home Page tab of the dialog box that appears. Figure 11.5 shows the Home Page tab of the Properties dialog box. You can also associate a Web page with a Folder using the MAPIFolder object's WebViewURL property. If you check Show Home Page by Default for This Folder or set the MAPIFolder object's WebViewOn property to TRue, users are shown the Web page when they select the folder, rather than an Outlook view of the items in the folder. Figure 11.5. Associating an HTML page with a folder.
You can get to the HTML document object model for the Web page displayed by a folder by using the Explorer object's HTMLDocument property. This property returns a non-Nothing value only if the selected folder is associated with a Web page. Interacting with the HTML document object model of a Web page through this property is an advanced topic that is not covered further in this book. |
Категории