I have to agree with Walker. I am not sure of what, EXACTLY, your situation is. A comma followed by a paragraph mark seems odd, and if the purpose is to converted to a tab-delimited file, this would be an issue.
"“.Collapse Direction:=wdCollapseEnd’
I don’t understand the need for the above statement.
Why would you have to “Collapse” anything to continue the do … loop?"
May I ask...did you try commenting it out and actually see
for yourself what the result is? Hmmm? That would be a first step in understanding. Or at least understanding what is the real question.
I will try again, although I thought I had clearly stated what is going on.
The search string: .Text = "[!0-9]," Do you understand what that actually means? It means: ANY character is that is NOT 0 to 9. The "!" means not. So...
"a," will be found.
"4," will not be found
"T," will be found
"8," will not be found.
Now, as I mentioned in my post:
"2. as the found range DOES include the character before the ",", move the Start one character. This makes the range ONLY the ",""
what is actually found is "x," - a comma preceded by ANY character NOT 0 to 9. So, again:
"a," will be found; "H," will be found. The Range
becomes that. But...you do not really want the character before the comma, now do you? You NEED that character in order to check if it is 0 to 9, but after you find it (a character NOT 0 to 9 followed by a comma) you do not want to do anything with that character.
So, you take the found range (eg. "a,"), and you Move the Start, in order to get JUST the comma. Here it is again.
"2. as the found range DOES include the character before the ",", move the Start one character.
This makes the range ONLY the ",""
Now the Range is just the comma. You make the range a Tab. Be very clear about this.
The Range object is now that Tab.
The Do loop is functioning
on the range object (r).
Code:
With r.Find
.MatchWildcards = True
.Text = "[!0-9],"
Do While .Execute(Forward:=True) = True
r
is the Tab....so, as far as the Do loop is concerned, there IS no more commas, there IS no Forward.
If you Collapse the range object to a point, then there IS a Forward. Thus, the loop can continue.
Try commenting out the Collapse line. You will see that it will find the first character/comma....and that will be that. It will not find any more.
It is crucial to undertsand that a Range.Find
resizes the range
itself to the .Found (if found). Now if you are performing action ON THAT RANGE, there is no need to Collapse. However, if you perform action on PART OF that range, you do have to collapse. Why? Because you are changing the Range.Start and End. Check it out.
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.Text = "[!0-9],"
Do While .Execute(Forward:=True) = True
With r
.Font.Bold = True
End With
Loop
End With
Notice no Collapse? The code will perform action
against the entire range for each Found, and keep going. You are not doing anything to
boundaries of the range, just actioning the range itself.
"a,"
"G,"
"q,"
both the character and the comma will be bolded. The Range.Found will be actioned, and the code goes on to the next Found.
HOWEVER,
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.Text = "[!0-9],"
Do While .Execute(Forward:=True) = True
With r
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
End With
Loop
End With
would take the first "a," (character and a comma),
move (change) the Range, and action a bold on the comma. As there is NO collapse, the variable r (the Range) IS the comma...and there ain't no more Forward to look through.
If you DO collapse it, then the rest of the document IS Forward.
I know this is a little difficult to grasp at first, but once you do grasp what a Word Range really is, using them becomes a very powerful tool. Be careful not to confuse a Word range with an Excel range...they are VERY different!
faq219-2884
Gerry
My paintings and sculpture