Solved

Limit scope of search with VBA in Word

Posted on 2011-09-15
8
330 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now