Listing 11-1 showed how to use C#'s foreach keyword 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 either 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 to display a new Explorer window for. 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 to display an Inspector window for. 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, or false to show the Inspector as a modeless dialog. If you omit the parameter, it defaults to false.
Listing 11-4. A VSTO Add-In That Works with Explorer and Inspector Windows
private void ThisApplication_Startup(object sender, EventArgs e) { Outlook.MAPIFolder folder = this.Session. GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); // Create a new explorer Outlook.Explorer newExplorer = this.Explorers.Add( folder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal); newExplorer.Display(); string explorerIndex = newExplorer.Caption; // Get an explorer by passing a string and by passing an index Outlook.Explorer exp = this.Explorers[explorerIndex]; MessageBox.Show(String.Format( "Got explorer {0}.", exp.Caption)); exp = this.Explorers[1]; MessageBox.Show(String.Format( "Got explorer {0}.", exp.Caption)); // Create a new inspector object item = folder.Items[1]; Outlook.Inspector newInspector = this.Inspectors.Add(item); newInspector.Display(false); string inspectorIndex = newInspector.Caption; // Get an inspector by passing a string and by passing an index Outlook.Inspector inspector = this.Inspectors[inspectorIndex]; MessageBox.Show(String.Format( "Got inspector {0}.", inspector.Caption)); inspector = this.Inspectors[1]; MessageBox.Show(String.Format( "Got inspector {0}.", inspector.Caption)); }
Part One. An Introduction to VSTO
An Introduction to Office Programming
Introduction to Office Solutions
Part Two. Office Programming in .NET
Programming Excel
Working with Excel Events
Working with Excel Objects
Programming Word
Working with Word Events
Working with Word Objects
Programming Outlook
Working with Outlook Events
Working with Outlook Objects
Introduction to InfoPath
Part Three. Office Programming in VSTO
The VSTO Programming Model
Using Windows Forms in VSTO
Working with Actions Pane
Working with Smart Tags in VSTO
VSTO Data Programming
Server Data Scenarios
.NET Code Security
Deployment
Part Four. Advanced Office Programming
Working with XML in Excel
Working with XML in Word
Developing COM Add-Ins for Word and Excel
Creating Outlook Add-Ins with VSTO