Scaling Numbers in Excel How to Programatically Retrieve Data from a Database
Dec 10

While my earlier tips have used Excel examples, the name of this column is NOT Essential Excel Programming, so I suppose it is time to take a look at some useful tips related to other Office applications. One of the most useful properties in the Word object model is the Information property of the Selection object.

The Selection object represents the current selection in a window or pane. It is useful when you are writing macros that interact with the user.

When programming in Word, you frequently need to know things such as which page the user is working on, the number of pages in the document, the line number of the insertion point, the column number of the insertion point, and on and on.

Surprisingly, you only need to know one property to determine the answer to these questions and many more:

Selection.Information(wdInformation)

where wdInformation represents the information you want returned.

The Information property is unique, unlike other properties I have discussed in this column. The Information property is a parameterized property. Generally properties do not use parameters. The Information property, however, accepts a wdInformation enumeration consisting of 35 options, shown in Figure 1.


n44eopfigure1.jpg
Figure 1: Browsing Members of the wdInformation enumeration

As shown in Listing 1, using the Information property is very easy.

Listing 1: A simple demonstration of the Selection.Information property

Sub SelectionInformation()
Debug.Print "Selection starts at column: " & _
Selection.Information(wdFirstCharacterColumnNumber)

Debug.Print "Selection starts at line: " & _
Selection.Information(wdFirstCharacterLineNumber)

Debug.Print "The selection starts on page " & _
Selection.Information(wdActiveEndPageNumber) _
& ". There are " & _
Selection.Information(wdNumberOfPagesInDocument) & _
" page(s) in the document."
End Sub

To test this listing, enter the text =Rand(12,12) and press Enter to easily add a bunch to test data into a document. Then, experiment with the listing by placing the cursor at different locations in the document and running the macro. A sample of the output is shown below.

Listing 2: Example output from the SelectionInformation procedure

Selection starts at column: 11
Selection starts at line: 8
The selection starts on page 2. There are 5 page(s) in the document.

For details regarding the types of operations you can perform with Selection.Information and the correct enumeration value for each operation, visit

http://msdn2.microsoft.com/en-us/library/bb213848.aspx

Leave a Reply