Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-12-2013, 09:47 PM
tinfanide tinfanide is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office 2010 32bit
Expert
Range.Collapse?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default 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
Even without the line,
Code:
        oRange.Collapse Direction:=wdCollapseEnd
it is still the same in effect as with this line of codes.

Why is the range needed to be collapsed to the end then?
Reply With Quote
  #2  
Old 01-13-2013, 04:20 PM
fumei fumei is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

It doesn't.

BTW your code does not actually do anything.
Reply With Quote
  #3  
Old 01-15-2013, 10:08 PM
tinfanide tinfanide is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office 2010 32bit
Expert
Range.Collapse?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by fumei View Post
It doesn't.

BTW your code does not actually do anything.
The codes do something.
It FINDs the intended things and select them one by one.
Reply With Quote
  #4  
Old 01-16-2013, 12:12 PM
fumei fumei is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 01-16-2013, 06:43 PM
tinfanide tinfanide is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office 2010 32bit
Expert
Range.Collapse?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by fumei View Post
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.
Yes, I see. It's a bit hard to visualise your example though.
Thanks.
Reply With Quote
  #6  
Old 01-17-2013, 06:02 PM
fumei fumei is offline Range.Collapse? Windows 7 64bit Range.Collapse? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

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.
Reply With Quote
Reply



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
Range.Collapse? Drop-Down Menu, show-hide, expand-collapse??? 300zxmuro Word 1 02-27-2011 03:02 PM
Range.Collapse? Expand & Collapse Folders in "Unread Mail" WoodstockCEO Outlook 3 02-14-2010 09:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft