Special Enhancements to the Bookmark

In addition to enabling data binding and providing events, VSTO adds some additional special enhancements to the Bookmark control. VSTO combines an Interop bookmark with an Interop range, and therefore you will find many more properties and methods on a VSTO bookmark than can be found on an Interop bookmark. If you take a look in the Object Browser, you can see the differences. The right pane of Figure 6.20 shows all the members of the Interop bookmark.

Object Browser | ThisDocument.vb* ( Start Page f 5ampleDocument2.doc* ▼ X

Browse: My Solution "... ** ^ 1^1 "

<Search> t ¿J

Iff* Application() As Microsoft. Off ice. Interop. Word. Application T^f Column() As Boolean

Copy(String) As Microsoft. Off ice. Interop. Word.Bookmark |fj* Creator() As Integer =♦ DeleteO

Empty() As Boolean End() As Integer i^f NameQ As String ^ Parent() As Object

Range() As Microsoft. Off ice. Interop. Word. Range 4 SelectO

StartQ As Integer Iff* StoryTypeQ As Microsoft. Off ice. Interop. Word. WdStoryType

□•••{} Microsoft.Off ice. Interop. Word A >-o Addln a xo Addlns —o Adjustments ^ Application S ^ ApplicationEvents_SinkHelper E-^J ApplicationEvents2_SinkHelper S ^ ApplicationEvents3_SinkHelper S ^ ApplicationEvents4_SinkHelper

"0 AutoCaption S j-o AutoCaptions AutoCorrect S3 j-o AutoCorrectEntries 'O AutoCorrectEntry g ^o AutoTextEntries **o AutoTextEntry igJSSulIlii

*o Bookmarks *-o Border g ^o Borders

Figure 6.20. Properties and methods of an Interop bookmark

Figure 6.20. Properties and methods of an Interop bookmark

The right pane of Figure 6.21 shows all the members of the VSTO bookmark that can fit on the screen. Notice that the window can be scrolled, because there are many more methods, properties, and events on a VSTO bookmark. Take some time to scroll through all the members to familiarize yourself with them.

One important change concerns the Text property. Because the Bookmark and the Range objects are combined, the Text property is available directly as a member of the Bookmark control, rather than through the Range object. In other words, you can assign text to the bookmark using the following code:

Bookmarkl.Text = "sample"

JblUUy Udld

IIIUUW UJIIII

Object Browser ThisDocument.vb* | Start Page SampleDocument2.doc*

Browse: My Solution

Sb iJh

<Search>

E {} Microsoft.CSharp

{ } Microsoft. Office. Core

{} Microsoft. Office. Interop.'Word

E {} Microsoft. Office. Tools

% Action ij^l ActionClickEventHandler

ActionEventArgs j¿| BeforeCaptionShowEventHandler a

% ClickEventArgs ij^ ClickEventHandler ^ ContextChangeEventArgs sJ ContextChangeEventHandler B-^tf ControlCollection E-ControlCollectionEnumerator E % Document

MailMergeAfterMergeEventArgs (id MailMergeAfterMergeEventHandler MailMergeWizardStateChangeEventArgs MailMergeWizardStateChangeEventHandler ^ NodelnsertAndDeleteEventArgs Qj NodelnsertAndDeleteEventHandler B % OLEControl E-SaveEventArgs

SaveEventHandler ^ SelectionEventArgs J^) SelectionEventHandler E % SmartTag E-WindowEventArgs i WindowEventHandler EXMLNode E % XMLNodes

{ } Microsoft. Office. Tools. Word. Controls {} Microsoft.SqlServer.Server © {} Microsoft.Vbe,Interop {} Microsoft.VisualBasic

iff* Application() As Microsoft.Off ice. Interop. Word. Application AutoFormatQ

^ Bef oreDoubleClickj(Objectj Microsoft .Of f ice. Tools. Word. ClickEventArgs) j BeforeRightClickiObject, Microsoft. Of f ice .Tools. Word. ClickEventArgs)

BindingContextO As System.Windows.Forms.BindingContext $ BindingContextChanged(Object, System.EventArgs) g4 BoldQ As Integer ^ BoldBi() As Integer

BookmarkID() As Integer iff* Bookmarks() As Microsoft.Off ice. Interop. Word.Bookmarks iff* Borders() As Microsoft. Off ice. Interop. Word. Borders

Calculated) As Single ;§f* Case() As Microsoft. Off ice. Interop. Word. WdCharacterCase ^ Cells() As Microsoft. Off ice. Interop. Word. Cells j^f* Characters() As Microsoft, Off ice. Interop. Word, Characters g* CharacterWidth() As Microsoft. Off ice. Interop. Word. WdCharacterWidth CheckGrammar()

=<> CheckSpelling([ByRef Object], [ByRef Object], [ByRef Object], [ByRef Object], CheckSynonyms() Collapse([ByRef Object]) ColumnQ As Boolean

ColumnsQ As Microsoft. Off ice. Interop. Word. Columns CombineCharacters() As Boolean jff* CommentsQ As Microsoft. Off ice. Interop. Word. Comments

ComputeStatistics(Microsoft. Off ice. Interop. Word. WdStatistic) As Integer Container() As System.ComponentModel.IContainer ConvertHangulAndHanja([ByRef Object], [ByRef Object], [ByRef Object], [ByR

V ConvertToTable([ByRef Object], [ByRef Object], [ByRef Object], [ByRef Objed

^ Copy(String) As Microsoft, Off ice. Interop. Word, Bookmark CopyAsPicture()

fy CreateObjRef(System.Type) As System.Runtime.Remoting.ObjRef

$ CreatePublisher([ByRef Object], [ByRef Object], [ByRef Object], [ByRef Object CreatorQ As Integer

Cuto

DataBindings() As System, Windows. Forms. ControlBindingsCollection

DeleteQ v

Figure 6.21. Properties, methods, and events of a VSTO bookmark

This is as opposed to the code you would have to write for an Interop bookmark:

Bookmarks("Bookmark1").Range.Text = "sample"

What is interesting about the Text property of a VSTO bookmark is that the text you assign to this property does not delete the bookmark. When you use VBA to assign text to the bookmark, you must first set a range on the bookmark so that when you assign text (and as a result delete the bookmark), you can easily add the bookmark back to the document at the range that was set. This is still the case if you access the Text property from the Range of a Bookmark control (Bookmark1.Range.Text). However, if you assign text directly to the Text property of the Bookmark control (Bookmarkl.Text), the text is assigned and the bookmark remains intact, as you would expect.

Although bookmarks in general are still fragile—it is easy for an end user to delete a bookmark, and no event is raised when it happens— VSTO does help increase a bookmark's stability by ensuring that it is not deleted when you make an assignment.

0 0

Post a comment