How to Put All Document Words into an Array Using VBA in Word
Question details
The user wants to extract all words from a Word document and store them in a VBA array for further processing or text analysis.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to read, process, or analyze the text content within a specific Word document.
- Observed behavior
- The goal is to accurately count, extract, trim, format to lowercase, and store each word from the document's text into a structured string array.
Ensure you have enabled the Developer tab in your Word ribbon to access the VBA editor. It is also highly recommended to save a copy of your document before running new macros.
Loop Through the ActiveDocument.Words Collection
This method offers precise control, allowing you to format and trim each word individually before storing it in a correctly sized dynamic array.
Using Word's built-in Words collection allows you to accurately count the total number of words in the document, resize your array to fit, and iterate through the content systematically.
Press ALT + F11 to open the Visual Basic for Applications window. Insert a new Module from the Insert menu.
Set up a dynamic string array by typing 'Dim arrAllWords() As String'. Also, declare a variable for the word string like 'Dim strWord As String', and an integer counter.
Count the total words using 'lngWordsTotal = ActiveDocument.Words.Count' and resize the array by executing 'ReDim arrAllWords(lngWordsTotal)'.
Use a 'For Each objWord In ActiveDocument.Words' loop. Inside the loop, trim and format the word using 'strWord = Trim(LCase(objWord))', assign it to 'arrAllWords(x) = strWord', and increment your counter.

Use the VBA Split Function on Document Text
A faster, more concise method that utilizes the Split function to divide the document's raw text by spaces, bypassing Word's default word parsing.
Automate Document Tasks Seamlessly with WPS Office
WPS Office Writer fully supports VBA and Macros, allowing you to run your Word word-array extraction scripts seamlessly. It provides a lightweight, highly compatible environment for automating your document processing tasks.
- 1. Download and Install: Download and install WPS Office on your computer from the official website.
- 2. Open Your Document: Open your Word document (.docx or .doc) in WPS Writer.
- 3. Access the VBA Editor: Navigate to the Tools tab and click on 'Macros' or press ALT + F11 to open the VBA editor.
- 4. Run Your Code: Paste your array extraction script into a new module and run it effortlessly.

Frequently Asked Questions
Does the ActiveDocument.Words collection include punctuation marks?
Yes, in Word VBA, the Words collection often treats punctuation marks and paragraph marks as individual words. If you only want text, you may need to add an If statement to filter out non-alphanumeric characters during your loop.
Why do I get a 'Subscript Out of Range' error when adding words to my array?
This error usually occurs if the dynamic array hasn't been resized correctly before use. Make sure you use the ReDim statement based on the ActiveDocument.Words.Count before attempting to populate the array elements inside your loop.
What is the difference between ActiveDocument.Words and the Split function?
ActiveDocument.Words relies on Word's built-in parsing engine, which uniquely counts punctuation and spacing. The Split function simply takes a continuous text string and breaks it apart based on a specified delimiter (like a space), making it faster but sometimes requiring more string manipulation to clean up formatting.




