When you run a VBA macro in Word that uses the Range.Find method to search for a specific string, the macro may report that the text was not found even though you can see it on the page. This often happens when the search term spans a page break or a section break. The Find method operates on a contiguous text range, and Word inserts hidden characters for page and section breaks that break the logical sequence of characters. This article explains why the Find method fails across page breaks and provides a reliable workaround using the StoryRange and the Find object’s Wrap property.
Key Takeaways: Word VBA Find Method and Page Breaks
- Range.Find on a single page range: Only searches the text between two page break characters, missing text that crosses the break.
- StoryRange for the main text story: Searches the entire document body including all page and section breaks, eliminating the break character problem.
- Find.Wrap = wdFindContinue: Tells Find to wrap from the start to the end of the range, which prevents early termination when the search starts after the target.
Why Word VBA Range.Find Fails Across Page Breaks
Word stores page breaks as special hidden characters in the text stream. When you assign a Range to a specific page using the GoTo method with wdGoToPage, the Range covers only the text between the previous page break and the next page break character. The page break character itself is not included in the Range. If your search term contains characters that would normally be adjacent in the logical text but are separated by the page break character, the Find method cannot see the complete string because the break character sits in the middle and is not part of the Range.
For example, consider the sentence “This is a test” that breaks across two pages as “This is a” on page 1 and “test” on page 2. The Range for page 1 contains “This is a” followed by the page break character. The Range for page 2 starts with “test”. The Find method searches each Range independently and never sees “This is a test” as a contiguous string.
The Role of the StoryRange Object
Word’s document structure includes multiple stories: the main text story, headers, footers, footnotes, endnotes, and more. The main text story (wdMainTextStory) contains all the text of the document body including page break characters, section break characters, and other hidden formatting marks. When you access the main text story via ActiveDocument.StoryRanges(wdMainTextStory), you get a Range that spans the entire logical text stream without page break boundaries. Using this StoryRange for the Find operation ensures that the search sees the complete text sequence.
How to Fix the Find Method for Cross-Page Searches
The solution involves three changes to your VBA code: use the main text story’s Range instead of a page-specific Range, set the Find object’s Wrap property to continue the search, and explicitly handle the case where the Find starts after the match location. Below are the detailed steps.
- Declare and set the StoryRange variable
UseDim rngStory As Word.Rangeand thenSet rngStory = ActiveDocument.StoryRanges(wdMainTextStory). This gives you a Range that covers the entire document body including all break characters. - Collapse the range to the start
CallrngStory.Collapse Direction:=wdCollapseStartto move the insertion point to the beginning of the document. This prevents the Find from starting in the middle of the text. - Configure the Find object
Set the Find’s Text property to your search string. Set.Wrap = wdFindContinueso that if the search reaches the end of the range without finding the text, it wraps around to the beginning and searches again. This ensures that a match that appears before the starting point is still found. - Execute the Find and check the result
UseWith rngStory.Findand call.Execute. After the call, checkrngStory.Find.Foundto see if the text was found. If found,rngStorynow refers to the matched text. - Handle multiple matches in a loop
If you need to find all occurrences, wrap the Execute call in a Do While loop. After each successful find, callrngStory.Collapse Direction:=wdCollapseEndand thenrngStory.Find.Executeagain to move past the match.
If Word VBA Find Still Misses Matches After the Fix
Find Fails When the Search Term Contains Nonprinting Characters
If your search string includes spaces, tabs, or other whitespace, the Find method may fail because Word normalizes whitespace differently across page breaks. To solve this, replace spaces in the search string with the wildcard pattern [ ^t^s] or use the .MatchWildcards = True property and search for Thisisatest where matches any characters including break characters.
Find Returns False When the Range Starts After the Target Text
If you do not collapse the range to the start before calling Execute, and the current selection or insertion point is after the target text, Find may return False even with Wrap set to wdFindContinue. The Wrap property only works when the range is collapsed to a point before the target. Always collapse to the start of the StoryRange before the first Find call.
Find Skips Matches in Headers or Footers
The main text story does not include headers, footers, or footnotes. If your search target is in a header, you must access that specific story via ActiveDocument.StoryRanges(wdPrimaryHeaderStory) or iterate through all story ranges. Use a loop over all items in ActiveDocument.StoryRanges and perform the Find on each story.
Word VBA Find Methods: Page Range vs StoryRange vs Document Range
| Item | Page Range (GoTo) | StoryRange (wdMainTextStory) | Document Range (ActiveDocument.Range) |
|---|---|---|---|
| Scope | Text between two page breaks | Entire main text story including breaks | Entire document including all stories |
| Includes page break characters | No | Yes | Yes |
| Finds cross-page matches | No | Yes | Yes, but may include header/footer text |
| Requires Wrap setting | Not needed if range contains the text | Recommended (wdFindContinue) | Recommended (wdFindContinue) |
| Best use case | Searching within a single page | Searching body text across breaks | Searching all document content |
The StoryRange approach is the most reliable for finding text that spans page breaks because it works on the logical text stream. The Document Range includes all stories, which can cause false positives if the same text appears in a header or footer. Choose the method that matches your search scope.
You can now write VBA macros that correctly find text across page breaks by using the main text story’s Range and setting the Wrap property to wdFindContinue. Next, test your macro on a document with intentional page breaks to confirm the fix. For advanced scenarios, consider using the MatchWildcards property with a pattern that matches any break character.