inconsistent Find Behaviour

H

Harish Sharma

Hi,

I have created a find macro to store all the text which are colored into a
list box as follows:

Sub FindColoredText()
Dim CURRENT_DOCUMENT As Range
Set CURRENT_DOCUMENT = ActiveDocument.Content

With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
UserForm1.WhiteList.AddItem (.Parent)
DoEvents
Loop
End With


UserForm1.Show
End Sub


However, the problem is that this do loop is working on one particular
document but in another document it is going into infinite loop. I want to
ensure that the find loop starts from begening of the document and exits at
the end of the document.

I shall be thankful for all your help.

thanks,
 
F

Fumei2 via OfficeKB.com

More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?
 
F

Fumei2 via OfficeKB.com

Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the suerform is not loaded yet. OR..
have you loaded it, Done a Hide?
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?
[quoted text clipped - 25 lines]
 
F

Fumei2 via OfficeKB.com

Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?
[quoted text clipped - 25 lines]
 
F

Fumei2 via OfficeKB.com

Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?
[quoted text clipped - 25 lines]
 
H

Harish Sharma

Thanks Fumie2 for your prompt response. UserForm1 and WhiteList is
automatically loaded during the startup. and the default is set as hidden, it
is enabled only after all the subroutines are completed.

I think I have found out what is causing this to run correctly in one file
and incorrectly in other file-- when a colored words are present in
paragraph, it runs correctly and when it is present in a table it goes into
infinite loop. Can you help me to fix it.

In the below subroutine, I have done UserForm1.Show for debuging purpose, it
is not desired here, I just need to add all the colored text into the list
box inside the loop and exit after all are added.

With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
UserForm1.WhiteList.AddItem (.Parent)
DoEvents
Loop
End With



WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?




Fumei2 via OfficeKB.com said:
Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?
[quoted text clipped - 25 lines]

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201002/1

.
 
P

Pesach Shelnitz

Hi Harish,

You can eliminate the infinite loop that starts when your macro finds
colored text within a table by moving the boundaries of your Range object
foward before starting the next cycle of the loop. One way to accomplish this
is to collapse the range and advance the start by one character. The
following modified version of your macro illustrates this without calling
methods of your UserForm object.

Sub FindColoredText()
Dim CURRENT_DOCUMENT As Range
Dim MyList As String

MyList = ""
Set CURRENT_DOCUMENT = ActiveDocument.Content
With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
MyList = MyList & .Parent & vbCrLf
CURRENT_DOCUMENT.Collapse Direction:=wdCollapseEnd
CURRENT_DOCUMENT.MoveStart Unit:=wdCharacter, Count:=1
Loop
End With
MsgBox MyList
Set CURRENT_DOCUMENT = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz
My Web site: http://makeofficework.com


Harish Sharma said:
Thanks Fumie2 for your prompt response. UserForm1 and WhiteList is
automatically loaded during the startup. and the default is set as hidden, it
is enabled only after all the subroutines are completed.

I think I have found out what is causing this to run correctly in one file
and incorrectly in other file-- when a colored words are present in
paragraph, it runs correctly and when it is present in a table it goes into
infinite loop. Can you help me to fix it.

In the below subroutine, I have done UserForm1.Show for debuging purpose, it
is not desired here, I just need to add all the colored text into the list
box inside the loop and exit after all are added.

With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
UserForm1.WhiteList.AddItem (.Parent)
DoEvents
Loop
End With



WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?




Fumei2 via OfficeKB.com said:
Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?

Hi,

[quoted text clipped - 25 lines]

thanks,

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201002/1

.
 
H

Harish Sharma

Pesach,

Thank you so much for the fix, it worked!

Harish

Pesach Shelnitz said:
Hi Harish,

You can eliminate the infinite loop that starts when your macro finds
colored text within a table by moving the boundaries of your Range object
foward before starting the next cycle of the loop. One way to accomplish this
is to collapse the range and advance the start by one character. The
following modified version of your macro illustrates this without calling
methods of your UserForm object.

Sub FindColoredText()
Dim CURRENT_DOCUMENT As Range
Dim MyList As String

MyList = ""
Set CURRENT_DOCUMENT = ActiveDocument.Content
With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
MyList = MyList & .Parent & vbCrLf
CURRENT_DOCUMENT.Collapse Direction:=wdCollapseEnd
CURRENT_DOCUMENT.MoveStart Unit:=wdCharacter, Count:=1
Loop
End With
MsgBox MyList
Set CURRENT_DOCUMENT = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz
My Web site: http://makeofficework.com


Harish Sharma said:
Thanks Fumie2 for your prompt response. UserForm1 and WhiteList is
automatically loaded during the startup. and the default is set as hidden, it
is enabled only after all the subroutines are completed.

I think I have found out what is causing this to run correctly in one file
and incorrectly in other file-- when a colored words are present in
paragraph, it runs correctly and when it is present in a table it goes into
infinite loop. Can you help me to fix it.

In the below subroutine, I have done UserForm1.Show for debuging purpose, it
is not desired here, I just need to add all the colored text into the list
box inside the loop and exit after all are added.

With CURRENT_DOCUMENT.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute(findText:="", Forward:=True, Format:=True) = True
UserForm1.WhiteList.AddItem (.Parent)
DoEvents
Loop
End With



WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?




Fumei2 via OfficeKB.com said:
Just thought of something. You have Userform1.WhiteList.Add BEFORE Userform1.
Show.

How can you add anything to WhiteList if the userform is not loaded yet. OR..
.have you loaded it, Done a Hide?

Fumei2 wrote:
More details required. This puts the .Found of the .Find into a listbox on a
userform.

What document has the userform? Is, in fact, the document you are searching
the document that has the userform? I am not following how another document
is involved.

What fires Sub FindColored? Something on the userform? Something else?

Hi,

[quoted text clipped - 25 lines]

thanks,

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201002/1

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top