Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)

[Previous] [Next]

The Inspector object is the other user interface object in Microsoft Outlook. The Inspector object corresponds to the window in which an Outlook item or form is displayed. An example of an Inspector object is shown in Figure C-3. The Inspector object is shown in the form for the Account Tracking application.

Figure C-3 An Outlook window showing the Account Tracking compose form. This window is considered an Inspector object because it is displaying an Outlook item.

The Inspector object provides many methods and properties that allow you tailor the appearance of your Outlook forms dynamically at run time. The way to access an Inspector object at run time is by either using the Application.ActiveInspector method or the GetInspector method of the implicit Item object.

Inspector Object Properties

The Inspector object has six properties that you should be familiar with. They are described in the following sections.

CommandBars Property

Like the Explorer object, the Inspector object supports the CommandBars property. However, the objects returned by this property differ—instead of returning the menus of the Outlook application, it returns the menus of the open Outlook item. With the CommandBars property, you can use the FindControl method to execute a menu command in Outlook. The following code example is taken from the Account Tracking application, which appears Chapter 7 of the book. To execute the New Letter To Contact functionality of the Outlook Contact item, the Account Tracking application opens the selected contact you picked and uses the CommandBars property on the Contact form, as shown in Figure C-4.

Figure C-4 The Account Tracking application. You select the contact you want to write a letter to, and then the New Letter To Contact command is called by using the CommandBars property of the contact item inspector.

'********************************************** 'Send a letter to the contact '********************************************** Sub cmdSendLettertoContact_Click set oListBox = oDefaultPage.Controls("lstContacts") if oListBox.ListIndex = -1 then MsgBox "No selected account contact. Please select one.", _ 48, "Send letter to Account Contact" else set oItem = oRestrictedContactItems(oListBox.ListIndex + 1) oItem.Display oItem.GetInspector.CommandBars.FindControl(,2498).Execute end if end Sub

CurrentItem Property

The CurrentItem property returns the item currently being displayed in the Inspector object. If no item is currently open, Outlook returns an error. You can use this property to quickly find the properties or use the methods of the currently displayed item in Outlook, as the following code demonstrates:

Sub CommandButton1_Click Msgbox Application.ActiveInspector.CurrentItem.Subject End sub

EditorType Property

The EditorType property returns a constant number that represents the type of editor used to create the item. The constants include olEditorText (1), olEditorHTML (2), olEditorRTF (3), and olEditorWord (4). By determining the type of editor, you can create specific code to manipulate the body text. The following code shows how to retrieve the EditorType property and display the name of the editor in a message box:

Sub CommandButton1_Click Set oActiveInspector = Application.ActiveInspector msgbox "The currently active item is " & _ oActiveInspector.CurrentItem.Subject txtMessage = "It was created using the following editor: " Select Case oActiveInspector.EditorType case 1 txtEditor = "Plain text" case 2 txtEditor = "HTML" case 3 txtEditor = "Rich Text" case 4 txtEditor = "Microsoft Word" end select msgbox txtMessage & txtEditor End Sub

HTMLEditor Property

The HTMLEditor property is available only on items where the EditorType is set to olEditorHTML (2). This property returns the HTML document object model of Microsoft Internet Explorer, so you can manipulate the HTML and its rendering directly from the Internet Explorer object model. You should not attempt to store the object that is returned by this property to use at a later point because the returned object might be only temporary. If that is the case, it would be destroyed by Outlook, invalidating your object reference. The following code shows an example of using the Internet Explorer object model to show the HTML source for the message in Outlook:

Sub CommandButton1_Click Set oIE = Item.GetInspector.HTMLEditor For i = 0 To oIE.all.Length - 1 strType = TypeName(oIE.all.Item(i)) if strType = "HTMLBody" then txtHTMLSource = chr(13) & oIE.all.Item(i).outerHTML msgbox "Some of the HTML Source for this " & _ "document is: " & chr(13) & txtHTMLSource exit sub end if Next End Sub

ModifiedFormPages Property

The ModifiedFormPages property is an important property in Outlook since it returns a Pages collection. The Pages collection represents the pages of the Outlook form that can be or have been modified. For example, if you customize the second page of a message form, this second page will be returned in the Pages collection, which is returned by the ModifiedFormPages property. If no pages have been modified on the form, the ModifiedFormPages property will return an empty collection. By using the children objects of this property—in particular, the Pages collection that is returned to you—you can dynamically change the appearance of your form. The following code example shows you how you can change the appearance and visibility of controls on your form by using this property. The Pages collection will be discussed in more detail later in this supplement.

sub commandbutton1_click 'Retrieve the custom page, which is the tab name set oCustomPage = item.GetInspector.ModifiedFormPages("P.2") 'Hide a control oCustomPage.Controls("CommandButton2").visible = False 'Change the color for the text to Red oCustomPage.Controls("CommandButton3").ForeColor = RGB(255,0,0) 'Change the caption oCustomPage.Controls("CommandButton3").Caption = "My new caption" end sub

WordEditor Property

This property is similar to the HTMLEditor property in that it returns the object library for the editing application for the message. However, this time, the Word document object model is returned rather than the Internet Explorer object model. You can then use the functionality of the Word object model to manipulate the text inside of the message. In order for this property to work, the EditorType property must be set to olEditorWord (4), and the IsWordMail method of the Inspector object also returns True. The object that you receive from Outlook is temporary, so do not store it and then attempt to use it again at a later time. This can cause unpredictable results in your application because Outlook might have destroyed the object that your reference is pointing to. The following code example shows you how to the use the WordEditor property in your applications:

Sub Commandbutton1_click Set oWord = Item.GetInspector.WordEditor 'Use the word object model to add a shape to the message Set oShape = oWord.Shapes.AddShape(96,10,10,200,200) End Sub

Inspector Object Methods

The Inspector object presents you with a number of methods, but four key methods are the most important for you to learn because you'll use them the most in your applications. The following section describes these four methods and how to use them.

HideFormPage Method

The HideFormPage method hides the selected form page in the inspector. Be careful when using this method and then saving the item, because Outlook saves the form definition with the item, and these types of edits turn the form into a one-off form. If Outlook does save the form definition with the item, any changes you make to the form will not be implemented in the item automatically—Outlook always uses the form definition stored within the item rather than pull the new form definition from the Exchange Server. The following code shows you how to hide a form page in an Outlook form:

Sub CommandButton1_Click Item.GetInspector.HideFormPage("Message") End Sub

ShowFormPage Method

To show a form page in your application, use the ShowFormPage method and pass in the name of the form page you wish to show. Again, using this method makes Outlook save the form definition with the item. The following code shows you how to hide a form page and then make it reappear in Outook:

Sub CommandButton1_Click Item.GetInspector.HideFormPage("Message") msgbox "Form Page Hidden" Item.GetInspector.ShowFormPage("Message") msgbox "Form Page is back" End Sub

SetCurrentFormPage Method

In addition to hiding and showing form pages, you can force Outlook to display a certain form page in the Inspector object. To use the SetCurrentFormPage method, just pass in the name of the form page that you want to show. The following code gives you an example of how to use this method:

Sub CommandButton1_Click Item.GetInspector.SetCurrentFormPage("Message") End Sub

IsWordMail Method

The IsWordMail method returns True if the current item is using Word as its e-mail editor. This method helps you determine whether the current item is using Word as its editor before you attempt to call the WordEditor property to automate Word to implement functionality in your application. The following code modifies the WordEditor code discussed previously in the section "WordEditor Property" to take advantage of the IsWordMail method:

Sub Commandbutton1_click 'Check to make sure the user is using WordMail if Item.GetInspector.IsWordMail then Set oWord = Item.GetInspector.WordEditor 'Use the word object model to add a shape to the message Set oShape = oWord.Shapes.AddShape(96,10,10,200,200) Else Msgbox "You are not using Microsoft Word as your editor!" End if End Sub

Категории