How to: Count Characters in Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

The first character in a document is at character position 0, which represents the insertion point. The last character position is equal to the total number of characters in the document. You can determine the number of characters in a document by using the Count property of the Characters collection.

All characters in the document are counted, including spaces, paragraph marks, and other characters that are normally hidden. Even a new, blank document returns a count of one character because it contains a paragraph mark.

To display the number of characters in a document-level customization

  1. Select the entire document.

    Dim rng As Word.Range = Me.Range(0, Me.Characters.Count)
    rng.Select()
    
    Word.Range rng = this.Content; 
    rng.Select(); 
    
  2. Display the number of characters in the document in a message box.

    MessageBox.Show("Characters: " & Me.Characters.Count.ToString())
    
    MessageBox.Show("Characters: " + this.Characters.Count.ToString());
    

To display the number of characters in an application-level add-in

  1. Select the entire document. The following example selects the active document.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range( _
        0, Me.Application.ActiveDocument.Characters.Count)
    rng.Select()
    
    Word.Range rng = this.Application.ActiveDocument.Content;
    rng.Select();
    
  2. Display the number of characters in the document in a message box.

    MessageBox.Show("Characters: " & Me.Application.ActiveDocument.Characters.Count.ToString())
    
    MessageBox.Show("Characters: " + 
        this.Application.ActiveDocument.Characters.Count.ToString());
    

See Also

Tasks

How to: Retrieve Start and End Characters in Ranges

How to: Define and Select Ranges in Documents