Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: Stopping a search at the end of a range

Status
Not open for further replies.

PinkeyNBrain

IS-IT--Management
Dec 12, 2006
279
US
Start with a MSWord template similar to:
word.docm said:
range_1
today_bla bla bla
unused_text
today_bla bla bla
range_2
may_or_maynot_be_used_bla bla bla
may_or_maynot_be_used_bla bla bla
may_or_maynot_be_used_bla bla bla
may_or_maynot_be_used_bla bla bla
range_3
may_or_maynot_be_used_bla bla bla
may_or_maynot_be_used_bla bla bla
range_eof

I select a range (say range_1) modifying text as current needs dictate (eg. today_bla bla bla) I then go back over the range and look for chunks of text I don't need (eg. unused_text), and want to delete the entire line. Here is my existing sub to work with this.
Code:
Dim curr_section_rng As Range
call_a_sub_that_selects_my_range
Set curr_section_rng = Selection.Range

With curr_section_rng.Find
   .Text = "unused_text"
   .Wrap = wdFindStop
End With

Do While curr_section_rng.Find.Execute
   sel_text = curr_section_rng.Text
   call_another_sub sel_text
Loop

What is happening is that I only want to work within the current range. The sub as written does not confine itself to curr_section_rng and keeps going into the remainder of the document. How do I keep the activity limited to curr_section_rng

BTW - This question is a follow-up to posting "Preserving select.find" a few days eariler.
 
Found something that works.
Code:
Dim curr_section_rng As Range
call_a_sub_that_selects_my_range
Set curr_section_rng = Selection.Range
[b]curr_section_rng_eor = curr_section_rng.End[/b]

With curr_section_rng.Find
   .Text = "target_text"
   .Wrap = wdFindStop
End With

Do While curr_section_rng.Find.Execute
   sel_text = curr_section_rng.Text
   [b]If curr_section_rng_eor < curr_section_rng.End Then Exit Do[/b]
   call_another_sub sel_text
Loop
To me, it emphasizes what I don't understand about using ranges. If anyone can point me in the direction of something more applicable to quality programing, I'd appreciate it.
 

You're doing fine! Unfortunately, perhaps, using Find and Replace is not the best way to learn about Ranges!

F&R in VBA does not behave in quite the same way as F&R in the UI. When F&R finds something the Range is redefined to be the found text and VBA does not remember what the original Range was - and, as you have found, just runs on past it. To constrain your search you must remember the original Range yourself - which is more or less what you are doing, so keep at it.

One thing to note is that if you are changing text, your saved range end point will be wrong - a way round this is to use another range as they dynamically adjust. Consider this crude example:

Code:
Dim R1 As Range, R2 As Range, R1_End As Long

Set R1 = Selection.Range
Set R2 = R1.Duplicate
R1_End = R1.End

With R1.Find
    .Text = "unused"
    If .Execute Then R1.Text = ""
    MsgBox "End of R1 (end of found - and deleted - text): " & R1.End & vbCr & _
           "End of R2 (end of original range adjusted for deleted text): " & R2.End & vbCr & _
           "Saved end position of R1 (just a number - no longer end position of R1) " & R1_End
End With



Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top