PDA

View Full Version : Solved: What makes this “Wend While” stop?



Ice-Tea-Jan
09-12-2010, 09:22 AM
What makes this “Wend While” stop?

I understand the general concepts of this code; it searches for a paragraph mark (using wildcard conditions); then backs up to replace the paragraph mark with a space.

The Wend While loop continues as long as the condition “n=True”; and that this “n=True” is set RIGHT AFTER the initial search string loads.

When stepping line by line in the VBA editor, I see the loop bounce between the While & Wend (the way it is designed to work).

My question: how does this loop stop (be false) if it continues to execute the search (n=Selection.Find.Execute) while inside of the Wend While loop?

I know it the loops stops, but to me (the obvious newbie) -- it looks like it keeps searching irrespective of finding anything.

What am I missing? :dunno

Thanks for any help.
Janet


Sub WildcardMacro()
'
Dim n As Boolean
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13[!.^13(A-Z)]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
n = True
While n = True
n = Selection.Find.Execute
If Len(Selection.Text) = 2 Then
Selection.Text = Right(Selection.Text, 1) & " "
End If
Selection.MoveRight Unit:=wdCharacter, Count:=2
Wend
'
End Sub

Tinbendr
09-12-2010, 10:27 AM
I'm usually not very good at explaining things like this but I'll get it a shot.

It's the magic fairies. :devil2: (Ok I've had my fun.)

The first time through the loop, N is set manually to True.

When the selection is moved at Selection.MoveRight Unit:=wdCharacter, Count:=2 the selection is collapsed. The next iteration through the loop, selection is just an insersion point, so n = Selection.Find.Execute
will return False.

Any particular reason you are using Selection? Or is this just an exercise.

gmaxey
09-12-2010, 11:59 AM
Janet,

I don't follow exactly what it is that you are searching for i.e., "^13[!.^13(A-Z)]" Can you give an example?

Regardless, use and all reference to "n" really isn't needed. Just use:



Sub WildcardMacro()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13[!.^13(A-Z)]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
While .Execute
If Len(Selection.Text) = 2 Then
Selection.Text = Right(Selection.Text, 1) & " "
End If
Selection.MoveRight Unit:=wdCharacter, Count:=2
Wend
End with
End Sub

fumei
09-13-2010, 12:12 PM
searches for a paragraph mark (using wildcard conditions); then backs up to replace the paragraph mark with a space.

If all you want to do is remove all paragraph marks (except of course the last one which can NOT be removed...EVER), then:
Sub Para2Space()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(Findtext:=Chr(13), Forward:=True) = True
If r.End <> ActiveDocument.Range.End Then
r.Text = " "
r.Collapse 0
Else
Exit Do
End If
Loop
End With
End Sub
All paragraph marks are changed to a space.

Ice-Tea-Jan
09-13-2010, 09:05 PM
Tinbendr:
You little :devil2: ! I’ll get even! :neener:
So . . . no movement occurs within the Wend While loop UNLESS the text is preselected via the search? Therefore, when we run out of “found” items (that selects the text) – we are left with the insertion point which stops the loop?


Gmaxey:
That code is not mine; I was simply studying it as a learning exercise.
I believe the author of this code wished to run sentences together into one paragraph via deleting the “end” paragraph marks that occurred at each line.
However, your code is MUCH more understandable (in my newbie mind)! Thanks for submitting it for me to learn and study.


Fumei:
I was just trying to learn from the code – which belongs to somebody else.
My newbie code would smack of taking a tank to the 7-11 store -- when in fact you could instead do the same task in a Volkswagen. J
I did know; however, that you could not remove the last paragraph marker in the file. I’m not quite so green in the Word application. I believe that is considered the EOF mark?
Thanks to ALL of you for your patience and your answers.
I will mark the thread solved.

Regards,
Janet

Tinbendr
09-15-2010, 04:39 AM
So . . . no movement occurs within the Wend While loop UNLESS the text is preselected via the search? Therefore, when we run out of “found” items (that selects the text) – we are left with the insertion point which stops the loop?
Yes, that's correct.

gmaxey
09-15-2010, 04:50 AM
Tinbindr,

I am not going to say that your statement is not correct for this specific case. However, the loop continues as long as n is true. n is true as long as the .Execute method returns true. The .Excecute method returns true as long as it continues to find the defined search string. That is why "n" is redundant in the code example.

fumei
09-15-2010, 09:32 AM
And it is much better to use Collapse, rather than:
Selection.MoveRight Unit:=wdCharacter, Count:=2

Tinbendr
09-15-2010, 11:38 AM
I am not going to say that your statement is not correct for this specific case. However, ...
Yes, of course, you are quite right.

This is a very poor example of how to perform this search in the first place.

David