The Range Object

     

The Range Object

If you've used VBA with Excel, you probably know that Excel has no separate object to represent a cell. Instead, a cell is considered to be just an instance of the generic Range class.

Along similar lines, Word has no separate objects for its most fundamental text units: the character and the word. Like Excel, Word considers these items to be instances of a generic class, which is also called the Range object. A Range object is defined as a continuous section of text in a document: a few characters in a row, a few words in a row, a few paragraphs in a row, or whatever. A range can be anything from a single character to an entire document, as long as the text within it is continuous.

There are two basic methods for returning a Range object: the Document object's Range method and the Range property.

The Range Method

The Document object has a Range method that lets you specify starting and ending points for a range. Here's the syntax:

  Document  .Range(  Start,End  ) 

Document

The Document object you want to work with.

Start

The starting character position. Note that the first character in a document is at position 0.

End

The ending character position. Note that this character is not included in the range.

For example, the following statements use the myRange object variable to store the first 100 characters (0 through 99) in the active document:

 Dim myRange As Range myRange = ActiveDocument.Range(0, 100) 

The Range Property

Many Word objects have a Range property that returns a Range object, including the Paragraph and Selection objects (discussed later). This is important because these objects lack certain properties and methods that are handy for manipulating text. For example, the Paragraph object doesn't have an Italic property. The Range object does, however, so you format a paragraph's font as italic programmatically by referring to its Range property, like so:

 ActiveDocument.Paragraphs(1).Range.Italic = True 

This statement formats the first paragraph in the active document with italic text. (I discuss the Paragraphs collection in a moment.)

Reading and Changing Range Text

The Range object has a Text property that returns the text in the specified range. You can also use the Text property to set the text within the specified range. For example, the following code fragment checks the text in a document called letter.doc to see whether the first four characters equal the string "Dear"; if so, the text is replaced with "Greetings":

 With Documents("letter.doc").Range.(0,4)     If .Text = "Dear" Then         .Text = "Greetings"     End If End With 

Formatting Text

The Range object's properties also include many of the standard text formatting commands. Here's a brief review of just a few of these properties:

Range .Bold ” Returns True if the specified Range is formatted entirely as bold, returns False if no part of the range is bold, and returns wdUndefined if only part of the range is formatted as bold. You can also set this property using True (for bolding), False (to remove bolding), or wdToggle (to toggle the current setting between True and False).

Range .Case ” Returns or sets the case of the specified Range . This property uses various wdCharacterCase constants, including wdLowerCase , wdTitleSentence , wdTitleWord , wdToggleCase , and wdUpperCase .

Range .Font ” Returns or sets a Font object that specifies the character formatting used in the Range .

Range .Italic ” Returns True if the specified Range is formatted entirely as italic, returns False if no part of the range is italic, and returns wdUndefined if only part of the range is formatted as italic. You can also set this property using True (for italics), False (to remove italics), or wdToggle (to toggle the current setting between True and False).

For example, the following code fragment takes the Range object of the active document's first paragraph and then sets Bold to True, Italic to True, and the case to wdTitleWord :

 With ActiveDocument.Paragraphs(1).Range     .Bold = True     .Italic = True     .Case = wdTitleWord End With 

If you want maximum control over the character formatting in a range, use the Font property, which returns a Font object. From there you can manipulate not only the Bold and Italic properties, but also the type size (the Size property), the color ( Color ), strike through ( StrikeThrough and DoubleStrikeThrough ), small caps ( SmallCaps ), and much more. Here's an example:

 With ActiveDocument.Range.Font     .Color = RGB(0, 0, 255)     .Size = 12     .SmallCaps = True End With 

Some Useful Range Object Methods

Because it's the fundamental text object, it's not surprising that the Range object boasts a large number of methods that you can use to manipulate text. Here are a few of the ones you'll use most often:

Range .Copy ” Copies the Range to the Clipboard.

Range .Cut ” Cuts the Range from the document and places it on the Clipboard.

Range .Delete ” If used without arguments, this method deletes the entire Range . However, you can fine-tune your deletions by using the following syntax:

  Range  .Delete(  Unit,Count  ) 

Range

The Range object containing the text you want to delete.

Unit

(optional) A constant that specifies whether you're deleting characters (use wdCharacter ) or entire words (use wdWord ). If you omit this argument, VBA assumes you're deleting characters.

Count

(optional) The number of units to delete. Use a positive number to delete forward; use a negative number to delete backward. (The default is 1.)

Range .InsertAfter ” Inserts text after the specified Range :

 Range.InsertAfter(  Text  ) 

Range

The Range object after which you want to insert the text.

Text

The text to insert.

Range .InsertBefore ” Inserts text before the specified Range :

  Range  .InsertBefore(  Text  ) 

Range

The Range object before which you want to insert the text.

Text

The text to insert.

Range .InsertParagraph ” Inserts a paragraph that replaces the specified Range .

Range .InsertParagraphAfter ” Inserts a paragraph after the specified Range .

Range .InsertParagraphBefore ” Inserts a paragraph before the specified Range .

Range .Paste ” Pastes the contents of the Clipboard at the current Range position. To avoid overwriting the currently selected Range , use the Collapse method before pasting.

Range .Select ” Selects the specified Range . See the next section to learn more about the resulting Selection object.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net