Solved

Limit scope of search with VBA in Word

Posted on 2011-09-15
8
329 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

757 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

18 Experts available now in Live!

Get 1:1 Help Now