Monday, February 8, 2010

Inserting and retrieving text from a Bookmark.

There are several methods of inserting text at/into a bookmark. The method you use depends on whether you need to retrieve the text from the bookmark at a later time.

Lets look at the more obvious ways of inserting text at a bookmark.

ActiveDocument.Bookmarks("myBookmark").Range.Text = "Inserted Text"

If the bookmark is a placeholder bookmark, the inserted text will look like this:

I Inserted Text

If the bookmark is an enclosing bookmark, it will be deleted, and the inserted text will appear in it's place.

ActiveDocument.Bookmarks("myBookmark").Range.InsertBefore _
"Inserted Text"

ActiveDocument.Bookmarks("myBookmark").Range.InsertAfter _
"Inserted Text"

With both these methods, if the bookmark is a placeholder bookmark, the text will be inserted after the bookmark:

I Inserted Text

With enclosing bookmarks (even if the bookmark only encloses a space), the following occurs:

InsertAfter – [ Original Text ] Inserted Text
InsertBefore – [ Inserted Text Original Text ]

In order to retrieve the text in a bookmark, the bookmark needs to be
an enclosing bookmark. Then you can use the following to retrieve the text from the bookmark:

strBookmark = ActiveDocument.Bookmarks("myBookmark").Range.Text

You have already seen how to add text to an enclosing bookmark using the InsertBefore method above. But what if you want to insert text into a placeholder bookmark (making it an enclosing bookmark) so that you can retrieve the text from it at a later time ? And what if the bookmark is already an enclosing bookmark but you want to replace the text inside it ? There is no single command in VBA to achieve this. What you need to do is replace the bookmark with the inserted text (the bookmark is deleted), then re-create the bookmark around the inserted text. The following code is an example of how this is done:

Dim bmRange As Range

Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range

bmRange.Text = "Inserted Text"

ActiveDocument.Bookmarks.Add _
Name:="myBookmark", _
Range:=bmRange.

Thanks!

Working with Bookmarks in VBA

Types of Bookmarks
The most important thing you need to know when working with bookmarks in Word is that there are two “types” of bookmarks – “placeholder” bookmarks and “enclosing” bookmarks.

Before we proceed, and whenever you work with bookmarks, you should turn on display of bookmarks by going to Tools | Options | View and selecting “Bookmarks”. This makes it easier to see what's actually happening.

(1) Placeholder Bookmarks
If you click somewhere in the document and insert a bookmark it will look like a beam I – this is a “placeholder” bookmark.

(2) Enclosing Bookmarks
Now, if you select some text and insert a bookmark it will look like the selected text is enclosed in square brackets ie: [selected text] – this is an “enclosing” bookmark.

What is VBA used for?

A common use of VBA is to add increased 'functionality' or some 'automation' to the various MS OFFICE programs.

For example, you could access a commonly used menu item that you commonly use, such as the 'Statistics' tab from the document 'Properties' item from the 'File' menu, which is not easily accessable, to be instantly accessed by a hot key.
Or, you could automate a series of commonly performed procedures or actions. For example, you might commonly enter data into a table then do some calculations and/or formmating of that data. VBA can be used to automatically do the calculations and/or formatting of this data.
To get some idea of what can be done using VBA, do a web search for 'VBA Addins'.

What is the 'ESSENCE' of VBA?

To understand the 'Essence' of VBA, you must understand that every part of a MS Office program is made up of 'Objects', and these Objects have 'Properties' that can be 'set' or 'altered' in response to a user initiated 'Event' such as a 'mouse click' or a 'keypress'.

In WORD for example, the 'Menu Bar' is an Object. The 'Header' and 'Footer' are objects. The whole document, individual pages, a paragraph, a sentence, a word, an individual character are also objects.

All of these Objects have 'Properties'.
Some of these properties are either 'true' of 'false'. For example, a selection of text could have its 'bold' property set to 'true'.
Some of these properties could have a 'numerical' value. For example, a selection of text could have its 'size' property set to '36'.
Some of these properties could have a 'text' value. For example, a selection of text could have its 'color' property set to 'wdRed'.

All of these Objects can react to 'Events'.
Using a mouse or a keyboard, the user initiates an 'Event'. For example, clicking on the 'Font Size' menu item in the main toolbar, will initiate the Event of the dropping down of the Font Size selection box.

VBA 'controls' this interection between 'Objects' and their 'Properties' in response to a user initiated 'Event'

What is VBA?

Visual Basic for Applications (VBA) is an implementation of Microsoft's Visual Basic, an event driven programming language and associated development environment which is built into most Microsoft Office applications (including Apple Mac OS versions), some other Microsoft applications such as Microsoft MapPoint and Microsoft Visio - a former independent application which was acquired by Microsoft; as well as being at least partially implemented in some other applications such as AutoCAD, WordPerfect and ESRI ArcGIS. It supersedes and expands on the capabilities of earlier application-specific macro programming languages such as Word's WordBasic, and can be used to control almost all aspects of the host application, including manipulating user interface features such as menus and toolbars and working with custom user forms or dialog boxes.