Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Find In Defined Range
(or overcoming "found where not looking" behavior in Word)
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

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.

The Problem:

To illustrate the problem, open a new Word document and type the following text:

found where not looking 1

Select and bookmark the first, third, and last instance of "Test" using bookmarks A, B and C as shown below.

found where not looking 2

found where not looking 3

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.

VBA Script:
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

Site Note icon 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.

The workaround:

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.

VBA Script:
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.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo