Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

Listing 11.1 showed how to use Visual Basic's For Each loop to iterate over the Explorers and the Inspectors collections. It is also possible to get to an Explorer or Inspector using the index operator () and passing an index as an Object. That index can be a 1-based index into the array of Explorers or Inspectors, or it can be a String index that is the caption of the Explorer or inspector window in the array. Listing 11.4 illustrates using both types of indices with the Explorers and Inspectors collections.

Listing 11.4 also illustrates how to create a new Inspector and Explorer window. Both the Explorers and Inspectors collections have an Add method. The Explorers collection's Add method takes a Folder parameter of type MAPIFolder, which is the folder for which to display a new Explorer window. It takes a second optional parameter of type OlFolderDisplayMode that enables you to set the initial display used in the newly created Explorer window. The Add method returns the newly created Explorer object. To show the newly created Explorer object, you must then call the Explorer object's Display method.

The Inspectors collection's Add method takes an Object parameter, which is the Outlook item for which to display an inspector window. In Listing 11.4, we get an Outlook item out of the Inbox folder and create an inspector window for it. To show the newly created Inspector object, you must then call the Inspector object's Display method, which takes an optional parameter called Modal of type Object to which you can pass true to show the Inspector as a modal dialog box or False to show the Inspector as a modeless dialog box. If you omit the parameter, the parameter defaults to False.

Listing 11.4. A VSTO Add-In That Works with Explorer and Inspector Windows

Public Class ThisApplication Private Sub ThisApplication_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup Dim folder As Outlook.MAPIFolder Folder = Me.Session.GetDefaultFolder( _ Outlook.OlDefaultFolders.olFolderInbox) ' Create a new explorer Dim NewExplorer As Outlook.Explorer = Me.Explorers.Add( _ folder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal) NewExplorer.Display() Dim explorerIndex As String = NewExplorer.Caption ' Get explorer by passing a String and an index Dim exp As Outlook.Explorer = Me.Explorers(explorerIndex) MsgBox(String.Format("Got explorer {0}.", exp.Caption)) exp = Me.Explorers(1) MsgBox(String.Format("Got explorer {0}.", exp.Caption)) ' Create a new inspector Dim item As Object = folder.Items(1) Dim NewInspector As Outlook.Inspector NewInspector = Me.Inspectors.Add(item) NewInspector.Display(False) Dim inspectorIndex As String = NewInspector.Caption ' Get inspector by passing a String as the index Dim inspector As Outlook.Inspector Inspector = Me.Inspectors(inspectorIndex) MsgBox(String.Format( _ "Got inspector {0}.", inspector.Caption)) inspector = Me.Inspectors(1) MsgBox(String.Format( _ "Got inspector {0}.", inspector.Caption)) End Sub End Class

Категории