Solved

Limit scope of search with VBA in Word

Posted on 2011-09-15
8
344 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
[X]
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
  • 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

630 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