How to Create a Word Macro to Search the First Paragraph Only
Question details
The user needs to create a Word macro that prompts for specific text, searches exclusively within the first paragraph of a document, and triggers the Right subroutine if the text is found.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating a targeted text search via VBA without executing a global search across the entire document.
- Observed behavior
- The macro successfully restricts the search range to the first paragraph, preventing wildcard or global searches from scanning the remaining pages.
Ensure that the Developer tab is enabled in your Word processor and that your macro security settings allow you to run custom VBA scripts.
Define a Restricted Range Object in VBA
Restrict your search to the first paragraph by defining a specific Range object. This prevents the Find method from scanning the rest of the document.
When you use the default Selection object to search, Word often continues searching past your intended area. By defining a duplicate Range object specifically locked to Paragraphs(1), you guarantee the search stays local to that single block of text.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer, right-click your document, select 'Insert', and click 'Module'.
Within your new macro, use the InputBox function to prompt the user for the string they want to search for and store it in a variable.
Declare a Range variable and set it to ActiveDocument.Paragraphs(1).Range to limit the search scope exclusively to the first paragraph.
Use the .Find method on your newly created Range object. Add an If statement checking if the execution returns True; if so, type 'Call Right' to trigger the subroutine, otherwise display a message box.

Automate Targeted Document Searches with WPS Writer Macros
WPS Office provides excellent support for VBA macros, allowing you to run custom scripts and automate targeted text searches efficiently.
- 1. Enable Developer Mode: Open WPS Writer and navigate to the Developer tab on the top ribbon to access macro tools.
- 2. Open the VBA Editor: Click on the 'Macros' button or press ALT + F11 to launch the integrated script editor.
- 3. Paste Your Script: Paste your restricted-range macro code into a new module and save your document as a macro-enabled format to reuse the automation.

Frequently Asked Questions
Why is my Find macro searching the entire document instead of just the first paragraph?
If you use the standard Selection.Find method without explicitly limiting the Range object, the search default allows scanning past the initial selection. Assigning the range to ActiveDocument.Paragraphs(1).Range isolates the search exclusively to that section.
Can I modify this macro to search a different specific paragraph?
Yes, you can change the index number in the Paragraphs collection. For example, using ActiveDocument.Paragraphs(3).Range will target and search only the third paragraph.
How do I call another subroutine if the searched text is successfully found?
Inside your macro, after the .Execute method returns True, simply type the word 'Call' followed by the name of the subroutine (e.g., Call Right) to trigger it automatically.




