Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

in document search Word '10 1

Status
Not open for further replies.

jlockley

Technical User
Nov 28, 2001
1,522
US
Have small hope, but thought I'd ask (you all know everything). I need to search in a long code document (transferred to work for simplicity's sake) for the validation of a field requested in a query.
That means searching for a previous instance of the exact term. I notice that there is no more option for "Find Previous" - which means exiting the search window, moving to the top of the list, then if successful, going to the last word and....in other words, a pain.

Is there any known way to effect a search for previous instances of a term? I have about 200 to check, and I am almost cross eyed by now.
 
Hi jlockley,

By adding the following macros tor Word's 'Normal' template and assigning the keyboard shortcuts the Alt-N (for next) and the othe to Alt-P (for previous) to them, navigating forward or backward for any selected word or string is as simple as pressing Alt-N or Alt-P.
Code:
Sub NavigateNext()
Dim StrFnd As String, bDirection As Boolean
StrFnd = Trim(Selection.Text)
If StrFnd = vbNullString Then
  MsgBox "No Text Selected: Please select a find string!", vbOKOnly, "Navigation Error"
  Exit Sub
End If
Selection.Collapse wdCollapseEnd
bDirection = True
Call Navigator(StrFnd, bDirection)
End Sub
Code:
Sub NavigatePrev()
Dim StrFnd As String, bDirection As Boolean
StrFnd = Trim(Selection.Text)
If StrFnd = vbNullString Then
  MsgBox "No Text Selected: Please select a find string!", vbOKOnly, "Navigation Error"
  Exit Sub
End If
Selection.Collapse wdCollapseStart
bDirection = False
Call Navigator(StrFnd, bDirection)
End Sub
Code:
Sub Navigator(StrFnd As String, bDirection As Boolean)
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Text = vbNullString
  .Text = StrFnd
  .Forward = bDirection
  .Wrap = wdFindStop
  .Format = False
  .MatchAllWordForms = False
  .MatchSoundsLike = False
  .MatchWildcards = False
  .Execute
  If .Found = False Then
    MsgBox Chr(34) & StrFnd & Chr(34) & " not found", vbExclamation
  End If
End With
End Sub
As you can see, there's actually three macros. The third macro (Navigator) is called by the other two and does the actual work of finding the next/previous instance.

As coded, the 'NavigateNext' and 'NavigatePrev' subs look for matches of only the actual string selected, which might only be a single character. Telling Word to use a whole word is as simple as double-clicking on that word before using the Alt-N or Alt-P. If you'd rather have Word act upon the whole of the currently-selected word if the selection is less than the complete word, you could add the following code to the 'NavigateNext' and 'NavigatePrev' subs between the 'Dim' and 'StrFnd' lines in each:
Code:
With Selection.Range
  If Len(Trim(.Text)) > 0 Then
    If .Words.Count <= 2 Then
      If Len(Trim(.Text)) <= Len(.Text) Then
        .Words.Last.Select
      End If
    End If
  End If
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
Great. I will do this. Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top