#1
|
|||
|
|||
Range.Collapse?
Code:
Sub FindUntiltheEndofDocument() Dim oRange As Range Set oRange = ThisDocument.Range(0, 0) With oRange.Find .ClearFormatting .MatchCase = False .Text = "cat" While .Execute oRange.Select '' Why is it needed? oRange.Collapse Direction:=wdCollapseEnd Wend Set oRange = Nothing End With End Sub Code:
oRange.Collapse Direction:=wdCollapseEnd Why is the range needed to be collapsed to the end then? |
#2
|
|||
|
|||
It doesn't.
BTW your code does not actually do anything. |
#3
|
|||
|
|||
The codes do something.
It FINDs the intended things and select them one by one. |
#4
|
|||
|
|||
Oooops, sorry. That is what I get having multiple forums open at the same time. I was responding to something else. My bad.
However, to answer the question, the Collapse itself does not really do anything...welll not technically true. It DOES collapse the range, but that itself has no real effect. Or rather, there is an effect but it makes no difference. During a Find operation, the actual range object is changed to each Found. This is happening. It then moves on to the next Found. You use a Collapse instruction when you CHANGE something in the Found. Say you find "cat", and you change the "c" to an "h". Because there is a change (and it more noticeable in a format rather than textual change), the Find operation starts up again at the START of the last Found (i.e "hat"). In this case, the Find will ignore "hat" (because the Find text is "cat"). It moves on searching for the next "cat". But suppose you had a Bold operation to the found - that is "cat" remains "cat", but bolded. In that case, when the change to the range is done, Find starts up again at the START of the last Found - i.e the SAME "cat" found. In which case the code keeps on finding the same instance of "cat". Thus the need for a Collapse. The "cat" is found, the bold is applied, the range is collapsed to the END of the found range, and starts again...thus going on to the NEXT "cat". You use a Collapse when there is a change to the found range. |
#5
|
|||
|
|||
Quote:
Thanks. |
#6
|
|||
|
|||
Hmmm, not sure how |I can describe it better.
A Range.Find (even if you make the entire document the range) when executed CHANGES the range with each Found. So, say you have: Blah blah blah cat blah blah blah yadda cat yadda cat. You make the range the whole thing. Now you do a Find for "cat". The first Found makes the range = the first cat. You can test this by getting the Start and End of the .Found. Continuing on, the Find operation starts at the END of current .Found. Thus the first "cat" is BEHIND the current search forward point. Thus the next .Found will be the second "cat". HOWEVER....if you do something with the .Found (i.e. the first "cat"), change something - say a Bold operation - then the Find operation starts at the START of the current .Found. Thus the first "cat" is IN FRONT of the current search point. Thus the next .Found will be the SAME "cat". It will loop. So, if you CHANGE the .Found, you need to use .Collapse to move the search forward point to the end of the .Found range. The most important point is that a .Find operation on a range changes the actual range Start and End numbers. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Expand/Collapse default settings not retained | PatP | Outlook | 8 | 07-25-2013 11:44 PM |
Expand/Collapse in Html Word File | kitkat0910 | Word | 0 | 10-10-2012 03:42 AM |
multiple text boxes collapse on move | apricoti | Word | 2 | 01-26-2012 05:55 PM |
Drop-Down Menu, show-hide, expand-collapse??? | 300zxmuro | Word | 1 | 02-27-2011 03:02 PM |
Expand & Collapse Folders in "Unread Mail" | WoodstockCEO | Outlook | 3 | 02-14-2010 09:11 PM |