Microsoft Word Characters with VBA (Visual Basic for Applications)

Microsoft Word Characters with VBA (Visual Basic for Applications)

Characters

Microsoft Word offers collection of characters exposed by Range object where each character can be iterated using an index (a positive integer). In this post we will cover couple of properties and methods to work with Characters collection in a document.

Count

Can be used to get the count for specific range in a document. Below code example will grab first paragraph of the document and shows total characters as result.

Code example

Public Sub CharactersCountExample()
    'Declare object to hold current document
    Dim oDocument As Document
    
    'Bind acitve document reference
    Set oDocument = ActiveDocument
    
    'Declare variable to hold first paragraph
    Dim oPraragraph As Paragraph
    
    'Bind reference of fist paragraph
    Set oPraragraph = oDocument.Paragraphs(1)
    
    'Declare object to hold paragraph range
    Dim oRange As Range
    
    'Bind paragraph range
    Set oRange = oPraragraph.Range
    
    'Print total number of characters
    MsgBox "First Paragraph contains total " & oRange.Characters.Count & " Characters"
    
    'Memory cleanup
    Set oRange = Nothing
    Set oPraragraph = Nothing
    Set oDocument = Nothing
End Sub

Output

First Property

It returns a character which refers to very first character of the specified range as shown below:

Public Sub FirstCharacterCountExample()
    'Declare object to hold current document
    Dim oDocument As Document
    
    'Bind acitve document reference
    Set oDocument = ActiveDocument
    
    'Declare variable to hold first paragraph
    Dim oPraragraph As Paragraph
    
    'Bind reference of fist paragraph
    Set oPraragraph = oDocument.Paragraphs(3)
    
    'Declare object to hold paragraph range
    Dim oRange As Range
    
    'Bind paragraph range
    Set oRange = oPraragraph.Range
    
    'Print First character
    MsgBox "First Character of your Paragraph is " & oRange.Characters.First
    
    'Memory cleanup
    Set oRange = Nothing
    Set oPraragraph = Nothing
    Set oDocument = Nothing
End Sub

Output

Last Property

Returns last character of given range, mostly it points to Carriage Return which is known as Character 13 in ASC standard, Hence to make the example clear I have printed last character in ASC format.

Code example

Public Sub LastCharacterCountExample()
    
    'Declare object to hold current selection
    Dim oRange As Range
    
    'Bind paragraph range
    Set oRange = Selection.Range
    
    'Print Last character
    MsgBox "Last Character of your Selection is " & Asc(oRange.Characters.Last)
    
    'Memory cleanup
    Set oRange = Nothing
End Sub

Output

Item Method

Helps to iterate Nth character by passing a positive integer value as index. Below code we will obtain 2nd character from the selection:

Code example

Public Sub LastCharacterCountExample()
    
    'Declare object to hold current selection
    Dim oRange As Range
    
    'Bind paragraph range
    Set oRange = Selection.Range
    
    'Print 2nd character of selection
    Dim i As Integer
    i = 2
    MsgBox "You want " & i & "nd Character i.e. " & oRange.Characters.Item(i)
    
    'Memory cleanup
    Set oRange = Nothing
End Sub

Output

Please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published. Required fields are marked *