Accessing a Host Control from an Underlying Interop Object

Each host control has an associated underlying Interop object. You can access this object from the host control by using its InnerObject method. However, there is no way to do the opposite: access a host control from the underlying Interop object. VSTO does not provide a collection for each of the host controls; instead, there is a single Controls collection, which can contain host controls as well as Windows Forms controls.

Although a VSTO Bookmarks collection does not exist, there is an Interop Bookmarks collection (every Bookmark control has an underlying Interop bookmark); therefore, you can loop through all Interop bookmarks in the document just as you can in VBA. An example is shown in Listing 6.11.

Listing 6.11. The Interop Bookmark collection

For Each bookmark As Microsoft.Office.Interop.Word.Bookmark _ In Me.Bookmarks

MsgBox(bookmark.Name.ToString())

Unfortunately, you cannot access any VSTO-specific functionality of the bookmark because they are Interop bookmarks; you do not have access to the data binding and events that VSTO provides. You cannot use a For Each statement for VSTO bookmarks because there isn't a VSTO Bookmarks collection. If you change the code in Listing 6.9 to declare the bookmark variable as the type Microsoft.Office.Tools.Word.Bookmark, the code will compile fine, but you will receive an error when you run the application.

As an alternative, you can use the Controls collection to loop through each control in the collection and then determine whether it is of the type you are interested in—in our case, a Bookmark host control. Listing 6.12 loops through all controls in the Controls collection and checks whether the control is of the type Microsoft.Office.Tools.Word.Bookmark. If the control is a Bookmark host control, its name is displayed in a message box. Because you declare myControl as an Object, you must cast the object back into a Bookmark host control before you can access its properties, methods, and events.

Listing 6.12. The Controls collection Dim myControl as Object

For Each myControl in Me.Controls

If TypeOf(myControl) Is Microsoft.Office.Tools.Word _ .Bookmark Then

Dim VSTOBookmark As Microsoft.Office.Tools.Word. _ Bookmark = CType((myControl), _ Microsoft.Office.Tools.Word.Bookmark)

MsgBox(VSTOBookmark.Name.ToString())

End If

This might seem like a lot of work to do each time you want to access a VSTO host control from the underlying Interop object. Instead, you could create a helper function for each host control so that you can obtain the corresponding host control whenever you have accessed the Interop object, as shown in Listing 6.13. You could create a class that contains all the helper functions and then add this class to each VSTO Word solution that you create. This function, and any other helper functions you create, can then be called from your code as needed.

Listing 6.13. Helper function for Bookmark host control

Private Function GetBookmarkHostControl(ByVal _ InteropBookmark As Word.Bookmark) As _ Microsoft.Office.Tools.Word.Bookmark

Dim myControl As Object = Nothing Dim VSTOBookmark As Microsoft.Office.Tools.Word .Bookmark = Nothing

' Loop through each control in the controls ' collection and check whether the current ' control is a bookmark. For Each myControl In Me.Controls

If TypeOf (myControl) Is Microsoft.Office. Tools.Word.Bookmark Then

' Cast the control into a VSTO Bookmark. VSTOBookmark = CType((myControl), _

Microsoft.Office.Tools.Word.Bookmark)

' If VSTO bookmark has the same name as the ' Interop bookmark, return it; otherwise, ' release the variable and check the next ' control.

If VSTOBookmark.Name = InteropBookmark.Name Then Exit For

Else

VSTOBookmark = Nothing End If

End If

Next

Return VSTOBookmark End Function

0 0

Post a comment