Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Limit scope of search with VBA in Word

Posted on 2011-09-15
8
335 Views
Last Modified: 2012-05-12
    If I select a paragraph in Word and do a search, if the search string is not in the selected paragraph Word asks if I want to search other areas of the document.

     How can I get VBA similarly to limit its search to a selected area rather than to go through the entire document?
     --JRA
0
Comment
Question by:JohnRobinAllen
  • 4
  • 4
8 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 total points
ID: 36542505
Have you tried wdFindStop?
With Selection.Find
        .Text = "Some Text"
        .Forward = True
        .Wrap = wdFindStop
        .Execute
    End With

Open in new window

0
 

Author Comment

by:JohnRobinAllen
ID: 36548675
The code works, sort of, in the sense that if the search is successful, Find.Found is set to True. If the search is unsuccessful, Find.Found is set to False. The information is almost useless unless I can find out where the search field is. The selected text remains selected. Nothing indicates where the search string is.

     Is there any value that will give me the location of the successful search? If not, I can repeat the search with a different .Wrap value.

     Regardless of what GrahamSkan answers, I will give full credit to the solution he proposed, but I'll leave the question open for a day or two pending a possible better solution to the location problem that apparently will require a second search with a different .Wrap value.

     JRA
0
 

Author Comment

by:JohnRobinAllen
ID: 36548784
On further examination, I now realize that my comment 36548675 above is not valid. I had Selection.Extend = True, and that prevented me from seeing the location of the successful search. When I insert a Selection.EscapeKey to cancel the Extend, the code works perfectly.

Thanks!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Closing Comment

by:JohnRobinAllen
ID: 36548793
As always, Graham Skan's solution works perfectly. Further comments are below in comment 36548784.

     Many Thanks!
     JRA
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36548912
Do you need to see the found text (as opposed to finding its range)? If not then you could use a range.find.

In my tests, if the text is found in the Selection, then the text is highlighted. Unfortunately, after a successful find, if nothing is done to change the Selection, it will then extend the find to the rest of the document.

This code shows one way of preventing that.


Sub SelFindTest()
    Dim rng As Range
    Set rng = Selection.Range
    With Selection.Find
        .Text = "galleries"
        .Forward = True
        .Wrap = wdFindStop
        MsgBox .Execute()
    End With
    rng.Select
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36548925
Cross-posted. I didn't test the EscapeKey method.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36548948
I have now tested the EscapeKey. It doesn't fix the symptom that I described, but whwhich wasn't a problem for you anyway.
0
 

Author Comment

by:JohnRobinAllen
ID: 36549431
Graham Skan, you are fantastic. Even after you have solved a problem and points are awarded, you give further details that refine the solution.
     Many, many thanks
     JRA
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question