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
 
Hi alikim,

Please use the code tags when posting code. That makes the code much easier to read.

I can't see why you need a loop. You could do the same with:
Code:
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
  .Execute Replace:=wdReplaceAll
End With
End Sub


Cheers
[MS MVP - Word]
 
Because I don't need to replace anything, I need to modify the text in a complicated way, depending on what it says.
 
I need to find all occurrences of bold text and change them individually.
Anyway, my question is why my code is not working, I'm not looking for a different solution.
 
It is impossible to tell why your code is not working unless you provide it. The cause is somewhere in here:
Code:
[green]' process information from selection[/green]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
I provided you the exact code, that line is just a comment.
Please read my first post.
 
The code you provided does not do what you say, in any circumstances I can think of. I presume, perhaps wrongly, that you actually have some other code where you have posted that comment, and I further presume that that code, or an interaction between that code and the posted code is the root cause of the problem. My presumption is strengthened by your statement in your second post that you need to do something complicated.

I have read your first post and it does not provide enough information to determine the cause of your problem. I know that line is just a comment and I was hoping you would expand on it. You are the one asking for help; at the moment I cannot give it.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
All your "presumptions" are wrong. This is the working code and it does what I said in my first post and I'm asking why.

If you don't have enough knowledge to explain don't post your meaningless replies here.
 
I paste the exact code that's not working and I'm asking why and you tell me that:

1. no, this code is working
2. no, it's not the real code and I have some other one.

I also have a presumption about you, a very short one. I won't post it here but it comes down to this:

Please ignore this thread, don't post anything here and do not waste my time.
 
Without wishing to be rude, I probably have far more knowledge of this than you; I'm sorry you find my replies meaningless.

Your code as posted runs just fine for me in both Word 2003 and Word 2007 and, and as I said, I cannot think of any circumstances in which it wouldn't. I'm sorry that I can't help further.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
The last paragraph is already a much nicer answer to my question, it would have been great it that was your only reply.

And if you removed the presumption that you "probably have far more knowledge" from the first paragraph it'd be just fine.

I'm using Word 2002 so that might be the reason.
 
alikim, If you have any doubts about Tony's ability to help you then have a look at his profile (click on his name at the top of his post). Then have a look at the tone of your posts. I feel that Tony is showing a great deal of patience in trying to help you.

faq219-2884

Gavin
 
To Ganova:
----------
When somebody starts making presumptions on my account straight away as to my ability to post the right code and also stating that it doesn't do what it does, I find it rude and utterly offensive so my tone is more than justified.

Who gives you any right to doubt my abilities?
The fact that I don't have profile on this forum?

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

If I ask a question it doesn't mean that you are smarter or have more knowledge than me so do not presume that either.

What kind of treatment is this?
Do you try to "condition" all new ppl here like that?

You are not trying to help, you are just trying to prove me wrong.

If you have nothing to say on the topic - don't. And don't lecture me.
 
Hi alikim,

Your intemperance is not helping your reputation here! Keep it up and you'll soon find no-one willing to consider anything you post.

The simple fact is that the macro you posted cannot of itself cause an endless loop.

Your rejoinder was that you:
"need to modify the text in a complicated way, depending on what it says"
OK, but there's nothing in you code to indicate what that might be or to support modifying the text based on user input. As TonyJollans correctly says:
The code you provided does not do what you say
Similarly, your claim:
"I don't need to replace anything"
is inconsistent with:
Code:
.Replacement.Text = ""
Likewise, your response:
"my question is why my code is not working"
doesn't help either. What is it that isn't working? What were you expecting to happen? Contary, to your claim:
It's not working on this particular file
I find no evidence to support your claim:
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
So unless you can give some more detail, preferably accompanied by a change in attitude, you're unlikely to get much help here.


Cheers
[MS MVP - Word]
 
Thank you for the document. I can confirm the problem you are seeing - the behaviour is slightly different depending on your version of Word. Interestingly Word 2010 appears to behave as Word 2003, rather than Word 2007.

I am looking at exactly what circumstances trigger the occurrence, and how best to work around it. Collapsing the Selection before each iteration helps in some versions, but not in Word 2007.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hi Tony,

If, as I suspect, the problem in Word 2007 is the highlighted end-of-cell marker (or an end-of-row marker), you could overcome it with:
Code:
Sub s()
With Selection
  With .Find
    .ClearFormatting
    .Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Font.Bold = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Do While .Find.Execute
    ' process information from selection
    If .Information(wdWithInTable) = True Then
      .MoveEnd wdCharacter, 1
      .Collapse (wdCollapseEnd)
    End If
  Loop
End With
End Sub
Whether this is applicable to Word 2002, which alikim uses, I don't know.


Cheers
[MS MVP - Word]
 
I have spent a little time playing with this and have not come up with anything that works in all situations in all versions of Word, and I can't really spend any more time on it today.

Whilst I consider this a bug in the VBA implementation of Find, I have to ask whether it reflects a real situation. What is it that you actually want to do to the bold text, and will you always be working solely with Tables? If this is just a situation that you fell on by chance, it may not stop you getting on with what you really want to do - in theory your code is good and works in most situations.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
I think ..

IF you are working in Word 2002 or Word 2003
AND you are searching from an Insertion Point Selection to the end of the document ...

THEN a check for the result of the Find operation being the same as the previous one should suffice as condition to break the loop if it doesn't break by itself ...

BUT it may be more complex depending on what is involved in your modifying the text in a complicated way



Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top