The Characters Object

     

The Characters Object

The Characters object is a collection that represents all the characters in whatever object is specified. For example, ActiveDocument.Paragraphs(1).Range.Characters is the collection of all the characters in the Range object given by ActiveDocument.Paragraphs(1).Range (the first paragraph in the active document). Other objects that have the Characters property are Document and Selection .

Because Characters is a collection, you refer to individual characters by including an index number ” Characters(50) , for example. The following statement formats the first character in the active document to point size 20:

 ActiveDocument.Characters(1).Font.Size = 20 

To count the number of characters in the specified object, use the Count property:

 totalChars = Documents("Chaptr07.doc").Characters.Count 

This example sets the variable totalChars equal to the number of characters in the Chaptr07.doc file.

Listing 7.5 shows another example that uses the Characters object. In this case, the function procedure named CountCharacters takes on an Object argument named countObject and a String argument named letter . The procedure determines the number of instances of letter that occur within countObject .

Listing 7.5. A Function that Counts the Number of Instances of a Specified Character in an Object
 Function CountCharacters(countObject As Object, letter As String) As Long     Dim i As Long, char As Range     i = 0     For Each char In countObject.Characters         If char = letter Then i = i + 1     Next 'char     CountCharacters = i End Function Sub TestCountCharacters()     MsgBox CountCharacters(ActiveDocument, "e") End Sub 


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