Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Limit scope of search with VBA in Word

Posted on 2011-09-15
8
Medium Priority
?
347 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 500 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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 

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

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

718 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