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



## Chris Macro (Jan 2, 2014)

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")


----------



## Macropod (Jan 2, 2014)

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:

```
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
```


----------



## Chris Macro (Jan 3, 2014)

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.


----------



## Macropod (Jan 3, 2014)

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?


----------



## ZVI (Jan 4, 2014)

Use this code in VBA of Word application.
 It puts text of selection in active Word’s document to VBA array.

```
' 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.

```
' 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
```


----------



## Chris Macro (Jan 6, 2014)

Vladimir, your first procedure for Word was exactly what I was looking for.  Thank you so much!


----------



## ZVI (Jan 6, 2014)

Hi Chris,
I am glad it helped!


----------



## KestrylR (Mar 7, 2017)

It was exactly what Ive been looking for all afternoon as well - thank you!


----------

