How to Find Text Only at the Start of a Paragraph Using Word VBA
Question details
The user wants a Word VBA macro to identify and target a specific word only if it is the very first word of a paragraph, ignoring occurrences of the same word elsewhere in the text.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting or text replacement where a specific word must only be modified if it begins a new paragraph.
- Observed behavior
- A macro loops through the document's paragraphs, successfully isolates the starting word of each, and manipulates the text (e.g., adding a space) without altering identical words buried mid-paragraph.
Ensure that the Developer tab is enabled in your Word ribbon and save a backup copy of your document before running custom macros to prevent unintended text modifications.
Loop Through Paragraphs Using a Range Object
This method iterates through the Paragraphs collection and evaluates the text of each paragraph individually, offering precise control over the starting text.
While standard Find and Replace can utilize wildcards to find text following a paragraph return, it often misses the very first paragraph of a document. Looping through paragraph objects and checking the text directly is much more reliable.
You must exclude the paragraph mark (Chr(13)) when capturing the range, otherwise text length calculations and precise string matching might fail.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, right-click your document name, hover over Insert, and click Module.
Declare a Paragraph and a Range variable. Set up a loop using 'For Each para In ActiveDocument.Paragraphs' to check every paragraph in the document.
Inside the loop, assign the paragraph's range to your Range variable (Set rng = para.Range). Next, move the end of the range back by one character (rng.MoveEnd Unit:=wdCharacter, Count:=-1) to exclude the invisible paragraph mark.
Use the 'InStr' or 'Left' function to check if the range begins with your target word (e.g., 'If InStr(1, rng.Text, "TargetWord", vbTextCompare) = 1'). If true, collapse the range to the end of the word and use 'rng.InsertAfter' to add your desired spacing or text.

Automate Tasks with WPS Office Macros
WPS Office provides robust built-in support for VBA macros, allowing you to easily write, edit, and execute scripts to manipulate text and automate complex document formatting tasks.
- 1. Open WPS Writer: Launch WPS Office and open your document in the Writer application.
- 2. Access the Developer Tools: Navigate to the Tools tab on the ribbon and click on Macros, or simply press Alt + F11 to launch the VBA Editor.
- 3. Paste Your Script: Insert a new module and paste your paragraph-looping VBA code into the workspace.
- 4. Execute the Macro: Press F5 or click the Run button to execute the code and instantly locate the target text at the start of your paragraphs.

Frequently Asked Questions
Can I use wildcard search to find a word at the beginning of a paragraph instead?
Yes, you can use the Find object with wildcards by searching for '^13TargetWord' (where ^13 represents a paragraph mark). However, this approach will fail to find the word if it is located in the very first paragraph of the document, as there is no preceding paragraph mark to trigger the match.
Why does my VBA text evaluation include a strange box character at the end?
That character represents the paragraph mark (carriage return). When you pull text from a paragraph's Range property, it includes this hidden character by default. You must shrink the range's end point by one character before evaluating the text to avoid this.
How can I adapt this macro to find a word at the beginning of a sentence?
Instead of iterating through 'ActiveDocument.Paragraphs', you can change your loop to iterate through 'ActiveDocument.Sentences'. You will use similar logic to evaluate the first word, but keep in mind that trailing spaces from the previous sentence might need to be trimmed.




