The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Help & Microsoft Word Tips page is to demonstrate an unexpected (buggy) behavior in Word when you attempt VBA Find operations within in a defined bookmark range. Special thanks to Word MVPs Jay Freeman and Tony Jollans for recalling to my memory this problem and the steps necessary to work around it.
To illustrate the problem, open a new Word document and type the following text:
Select and bookmark the first, third, and last instance of "Test" using bookmarks A, B and C as shown below.
Now run the following macro. Here we have declared a range and a counter variable. We first set the range = to the bookmark "A" range and perform a find operation using the defined range and search term. Next we repeat the find operation using the bookmark "B" range and finally using the bookmark "C" range. In each case we expect to find the defined search text one time.
Sub FindInBookMarkRange() Dim oRng As Word.Range Dim i As Long Set oRng = ActiveDocument.Bookmarks("A").Range With oRng.Find .Text = "Test" While .Execute i = i + 1 Wend End With MsgBox "Found " & i & " times." i = 0 Set oRng = ActiveDocument.Bookmarks("B").Range With oRng.Find .Text = "Test" While .Execute i = i + 1 Wend End With MsgBox "Found " & i & " times." i = 0 Set oRng = ActiveDocument.Bookmarks("C").Range With oRng.Find .Text = "Test" While .Execute i = 1 + 1 Wend End With MsgBox "Found " & i & " times." lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
Needless to say the results are not as you would anticipate or expect.
I attempt to fully explain this and other fickle behavior when using the VBA Find .property in my Word's Fickle VBA Find .Property tips page. Please visit that page if you need a complete review. The behaviors influencing the results returned above are:
You can overcome the first issue and take advantage of the second issue by collapsing the defined search range to its beginning.
You can overcome the second by processing only found search terms that are contained within the defined bookmark range.
Run the modified code shown below to see the results displayed as expected.
Sub FindInBookMarkRangeII() Dim oRng As Word.Range Dim i As Long Set oRng = ActiveDocument.Bookmarks("A").Range 'Collapse the range. oRng.Collapse wdCollapseStart With oRng.Find .Text = "Test" 'Process (count) only instances found in defined search range. While .Execute And oRng.InRange(ActiveDocument.Bookmarks("A").Range) i = i + 1 Wend End With MsgBox "Found " & i & " time." i = 0 Set oRng = ActiveDocument.Bookmarks("B").Range oRng.Collapse wdCollapseStart With oRng.Find .Text = "Test" While .Execute And oRng.InRange(ActiveDocument.Bookmarks("B").Range) i = i + 1 Wend End With MsgBox "Found " & i & " time." i = 0 Set oRng = ActiveDocument.Bookmarks("C").Range oRng.Collapse wdCollapseStart With oRng.Find .Text = "Test" While .Execute And oRng.InRange(ActiveDocument.Bookmarks("C").Range) i = i + 1 Wend End With MsgBox "Found " & i & " time." lbl_Exit: Exit Sub End Sub
That's it! I hope you have found this tips page useful and informative.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!