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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selection.Find.Execute loop doesn't stop

Status
Not open for further replies.

alikim

Programmer
Nov 27, 2006
10
AU
I wrote a small macros to process all bold pieces of text in the word document. I use break point on the loop statement to see how it goes, the script finds all bold pieces OK but when it comes to the last piece the loop won't stop, it goes forever selecting the last piece over and over again regardless wdFindStop parameter.

Can anyone please tell me how to fix it? thanks.

Sub s()
With Selection.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.Font.Bold = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
' process information from selection
Loop
End Sub
 
1. your tone is NOT justified as I just looked at your document and code, and you do NOT know what you are doing. It is not hypercritical of any of us to point out your errors.

2. " it goes forever selecting the last piece over and over again regardless wdFindStop parameter."

It goes on forever (an infinite loop) BECAUSE you do not know what you are doing. I use 2002, and I fixed it in seconds. But you know what? I am not going to post a solution because of your attitude and rudeness.

Once glance at your document file is enough to show me that at even a basic level you do not know what you are doing very well. We would help, and I would help you, but criticizing (rudely) Tony puts you over the top. He was trying to help, and you not only brushed him off, but in such an arrogant manner that I find unacceptable. As macropod suggested, take a loop at Tony's profile. He, and macropod, are both internationally recognized as being superbly knowledgable about Word. Further, they are both well recognized for their help and contributions to literally thousands of people. neither was rude to you whatsoever; they simply asked pertinent questions, trying to help you.

And not just Tony. macropod asked about replacing and you responded with:
Because I don't need to replace anything, I need to modify the text in a complicated way, depending on what it says. {/quote]

What? Are you so unaware that "modify the text" is a replace? If you modify text into something else, that IS a replacement. And if you can not even understand that - AND respond so rudely - then you do not deserve any answer.

To repeat, I fixed your file, so it does not do a repeating loop in a few seconds. I made my own "complicated" modification of text just to test things. It can be made to work with just about any "complicated" way you like. Figure it out yourself if you are going to be so rude. Here is a Hint though. It makes an infinite loop because you wrote it as an infinite loop.
You are not trying to help, you are just trying to prove me wrong.
We ARE trying to help. Proving you wrong is child's play and not actually worth much effort. If you would:

1. answer questions when asked, for example, what EXACTLY is going on in your
Code:
' process information from selection
Tony asked for more information, and you did not answer. This is YOUR problem.

2. "Why do you think you have a right to tell me I posted incorrect information here? "

That is simple. When you DO post such. It is not a "right", it is simply an accurate and correct statement.

To reiterate. Yes, you are correct, your partial code - since it does not actual do anything to the found bolded text and you have been asked about it, but did not answer - does indeed repeat the loop. It is doing things precisely as you have told it to do. You have coded it (as it is in the sample document you posted) to make an infinite loop. It will continue to do so until you code it to NOT do an infinite loop.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Oh, and:
Anyway, my question is why my code is not working, I'm not looking for a different solution.

ANSWER: your code is not working because you wrote it incorrectly.

A different solution would make it work. As it is NOT working, it sure seems to me that something "different" is required.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Oh, and just to detail the "complicated" modifications I used to test...

If "Test1" - change the text to "yadda_1" and make it italic

If "Test2" - change "Test2" to the text of the second bookmark of the first 15 files in the folder c:\zzz\blah

If "Test3" - "modify" - ahem, replace - the text ("Test3") to the text of each bookmark of all files in c:\zzz\whatever, alternating each between bold and italic

If "Test4" - "modify" - replace - the text with data retrieved from an Excel file

If "Test5" - "modify" the text - replace - to the saved filenames of 7 [n]new[/b] files created with each new file listing the files in 7 different folders, with a graphic inserted between each listing.

I have no doubt that your "complicated" requirement is more complicated than that, however, the above worked prefectly for me.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
That should work.
1-Select the beginning of the document
2- Set the find criteria
--Start the Loop
3- Find with forward ‘Execute’
4- Do your change
5- Set the selection after the location that you changed
--End the Loop
 
This is the code
Sub Macro4()
Dim start1 As Integer
Dim start2 As Integer
Dim rng As Range
With Selection.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.Font.Bold = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
start1 = Selection.Range.Start
Do While True
.Execute
If .Found Then
Set rng = Selection.Range
start2 = rng.Start
If start2 < start1 Then
Exit Do
End If
'Start your Change
rng.Text = "LOL"
'End Your Change
rng.Select
Else
Exit Do
End If
Loop
End With
activeDocument.Range(start1, start1).Select
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top