Others

How to Put All Document Words into an Array Using VBA in Word

Algirdas JasaitisAlgirdas Jasaitis Nov 3, 2026 868 views

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.

How to Put All Document Words into a VBA Array in Word
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications window. Insert a new Module from the Insert menu.

2
Declare Variables

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.

3
Size the Array

Count the total words using 'lngWordsTotal = ActiveDocument.Words.Count' and resize the array by executing 'ReDim arrAllWords(lngWordsTotal)'.

4
Loop and Store

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.

Loop Through the ActiveDocument.Words Collection
Handling Punctuation: Word's Words collection often counts punctuation marks as individual words. You may need to add an If statement inside your loop to exclude non-alphanumeric characters if you only want actual words.
Advanced Document Automation

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. 1. Download and Install: Download and install WPS Office on your computer from the official website.
  2. 2. Open Your Document: Open your Word document (.docx or .doc) in WPS Writer.
  3. 3. Access the VBA Editor: Navigate to the Tools tab and click on 'Macros' or press ALT + F11 to open the VBA editor.
  4. 4. Run Your Code: Paste your array extraction script into a new module and run it effortlessly.
Fully compatible with Microsoft Word (.docx, .doc) formatsAdvanced Macro and VBA support to automate complex tasksLightweight application with high performance and low resource usageFree to use with a familiar, easy-to-navigate interface
microsoft office alternative - wps office

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.