Word VBA, Read All Lines

In this article I will explain how you can use VBA to read every line in a word document and store the values in a string array/collection.


Method 1:

In this method the VBA code will select the entire document. After selecting the entire document the lines will be split using the String.Split() function:

Sub Example()
Dim strAll As String
Dim arrString() As String
Selection.WholeStory
strAll = Selection.Range.Text
arrString = Strings.Split(strAll, vbCr)
End Sub

Assume we have the following data in the document:

Word VBA, Data in document
Result after running the code:

Word VBA, Result
The first line selects the whole document:

Selection.WholeStory

The next line assigns the text to a string variable:

strAll = Selection.Range.Text

The last line of code uses the String.Split() function to split the string based on the vbcr delimiter:

arrString = Strings.Split(strAll, vbCr)

For more information about the String.Split() function please see the link below. Although the link is for Excel, its concepts are applicable to VBA for Word:


Method 2:

In this method each line is read separately into the string array:

Sub example2()
Dim strLine As String
Dim colString As Collection
Dim intLastLine As Integer
Dim intLastPage As Integer
Dim flag As Boolean

'get the last line number
Selection.EndKey unit:=wdStory
intLastLine = _
Selection.Range.Information(wdFirstCharacterLineNumber)
intLastPage = _
Selection.Range.Information(wdActiveEndPageNumber)
'initialize the string collection
Set colString = New Collection
'move back to the start of the document
Selection.HomeKey unit:=wdStory

'loop until the end of the document is reached
flag = True
While flag = True
'check if the end of the document has been reached
If (Selection.Range.Information(wdFirstCharacterLineNumber) _
= intLastLine) And intLastPage = _
Selection.Range.Information(wdActiveEndPageNumber) Then
flag = False
End If
'select the line
Selection.EndKey unit:=wdLine, Extend:=wdExtend
'get the line data
strLine = Selection.Range.Text
colString.Add (strLine)
'move down a line
Selection.MoveDown unit:=wdLine, Count:=1
Selection.HomeKey unit:=wdLine
Wend

End Sub

Result after running the code:

Word VBA Read All Lines Example 2 Result

The lines below move the cursor to the end of the document and get the last page and line number:

'get the last line number
Selection.EndKey unit:=wdStory
intLastLine = _
Selection.Range.Information(wdFirstCharacterLineNumber)
intLastPage = _
Selection.Range.Information(wdActiveEndPageNumber)

The while loop continues until the end of the document is reached:

While flag = True
...
Wend

The If statement checks if the end of the document has been reached:

If (Selection.Range.Information(wdFirstCharacterLineNumber) _
= intLastLine) And intLastPage = _
Selection.Range.Information(wdActiveEndPageNumber) Then
flag = False
End If

The lines below read the current line and adds the text string to the string collection:

'select the line
Selection.EndKey unit:=wdLine, Extend:=wdExtend
'get the line data
strLine = Selection.Range.Text
colString.Add (strLine)

The code below moves the cursor to the next line:

Selection.MoveDown unit:=wdLine, Count:=1
Selection.HomeKey unit:=wdLine

You can download the file and code related to this article from the link below

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com

Leave a Reply

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