VBA MS Word - Way to fill an Array with each line of text?

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I'm trying to figure out how to populate an Array in Word with each item containing a string of text and separated by a Return keystroke.

For Example:

Text in Word:

I walk my dog
I walk my cat. Hello!
Goodbye

Array Created in VBA:

Array("I walk my dog", "I walk my cat. Hello!", "Goodbye")
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You could do that, but why? After all, you can simply add the return to the data when extracted from the array.

FWIW, the following two subs generate the same output:
Code:
Sub Demo1()
Dim StrArry As Variant, i As Long
StrArry = Array("I walk my dog" & vbCr, "I walk my cat. Hello!" & vbCr, "Goodbye" & vbCr)
With ActiveDocument
  For i = 0 To UBound(StrArry)
    .Range.InsertAfter StrArry(i)
  Next
End With
End Sub

Sub Demo2()
Dim StrArry As Variant, i As Long
StrArry = Array("I walk my dog", "I walk my cat. Hello!", "Goodbye")
With ActiveDocument
  For i = 0 To UBound(StrArry)
    .Range.InsertAfter StrArry(i) & vbCr
  Next
End With
End Sub
 
Upvote 0
Hi Paul. I don't think I explained my question clearly. I have text in a Word document that I want to read into an array using VBA. I want each item in the created array to equal one line (or paragraph) from the text in the Word document.
 
Upvote 0
So how are you identifying the text you want to use? Please post the code, using the code tags on the 'Go Advanced' tab. Have you considered simply reading the text into a string variable and using the paragraph breaks already in the document as the element separators for subsequent processing?
 
Upvote 0
Use this code in VBA of Word application.
It puts text of selection in active Word’s document to VBA array.
Rich (BB code):

' Code in VBA Word
Sub Test1()
 
  Dim arr
 
  ' Put text of Word's selection to the array
  arr = Split(Selection.Range, vbCr)
 
  ' Show dimension of array
  Debug.Print "LBound = " & LBound(arr), "UBound = " & UBound(arr)
 
  ' Show each element of array
  For i = 0 To UBound(arr)
    Debug.Print i, arr(i)
  Next
 
  ' Join elements of array to the string with delimiter
  Debug.Print "Join = " & Join(arr, " | ")
 
End Sub


Put the below code into VBA of Excel.
It gets the text of active Word’s document ant puts it into VBA array.
Rich (BB code):

' Code in Excel VBA
Sub Test2()
 
  Dim objWordApp As Object
  Dim arr
 
  ' Try to find open Word Application
  On Error Resume Next
  Set objWordApp = GetObject(, "Word.Application")
  If Err Then MsgBox "Window of Word Application not found", vbExclamation, "Exit": Exit Sub
  On Error GoTo 0
 
  If objWordApp.Documents.Count > 0 Then
   
    ' Copy text of Word's active document to the array
    arr = Split(objWordApp.ActiveDocument.Range, vbCr)
   
    ' Show dimension of array
    Debug.Print "LBound = " & LBound(arr), "UBound = " & UBound(arr)
  
    ' Show each element of array
    For i = 0 To UBound(arr)
      Debug.Print i, arr(i)
    Next
   
    ' Join elements of array to the string with delimiter
    Debug.Print "Join = " & Join(arr, " | ")
 
  Else
     
    MsgBox "There are no open documents", vbExclamation, "Exit"
     
  End If

  Set objWordApp = Nothing
 
End Sub
 
Last edited:
Upvote 0
Vladimir, your first procedure for Word was exactly what I was looking for. Thank you so much!
 
Upvote 0
Hi Chris,
I am glad it helped!
:)
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,658
Members
452,575
Latest member
Fstick546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top