Find/Replace loop hangs in a table cell

E

Elessvie

Hello, people!

With much help from you kind folks, I've created a macro that searches
through a document and replaces text within quotations with text that is
bold.

This works great in documents without tables. But if a document has tables
sprinkled throughout, and a table cell has text within quotations, the macro
hangs there. I've watched with Step-Into, and the macro never seems to exit
the cell.

Are tables to be handled differently from documents? Any suggestions
(including pointing me to information on this) would be greatly appreciated!

Thank you,

-Lynne
 
G

Greg Maxey

Try:

Sub FindAndBoldTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = """<*>"""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub
 
E

Elessvie

Thank you very much for your response, Greg.

Your code is what I have, too (except inside the Find loop I use
..Text = "[^0034^0147]*[^0034^0148]"
instead of
..Text = """<*>""").

But I have the find loop being called from a button in a UserForm. The
button passes along a font attribute choice and the code uses Case statements
to assign that attribute to the found text, then continues looping through
the document.

It works great until it comes upon a table. Then, if the found text is
inside a cell, the loop just goes round and round inside that cell.

I haven't posted my code because I didn't want to impose on you. It looks
like I'm not coding properly for instances of Tables. I was hoping there was
some information about how tables are handled during a Find, but I can't find
any.

Am I on the right track, in thinking tables are the problem?

Thank you for whatever help you can offer.

-Lynne
 
G

Greg Maxey

Impose away. Without seeing your code is impossible to say what the issue
might be.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



Elessvie said:
Thank you very much for your response, Greg.

Your code is what I have, too (except inside the Find loop I use
.Text = "[^0034^0147]*[^0034^0148]"
instead of
.Text = """<*>""").

But I have the find loop being called from a button in a UserForm. The
button passes along a font attribute choice and the code uses Case
statements
to assign that attribute to the found text, then continues looping through
the document.

It works great until it comes upon a table. Then, if the found text is
inside a cell, the loop just goes round and round inside that cell.

I haven't posted my code because I didn't want to impose on you. It looks
like I'm not coding properly for instances of Tables. I was hoping there
was
some information about how tables are handled during a Find, but I can't
find
any.

Am I on the right track, in thinking tables are the problem?

Thank you for whatever help you can offer.

-Lynne



Greg Maxey said:
Try:

Sub FindAndBoldTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = """<*>"""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub
 
E

Elessvie

Hello, Greg -- Sorry for my delayed response! I was documenting my code for
you when I THINK I came across what was is causing my problem.

I won't be able to work on it for a day or so, so I'm posting now to thank
you for your time and to close this thread so that you are not left hanging
(if you are!).

I will post again if (more probably WHEN) I get into another dilemma!

Thanks again!

-Lynne

Greg Maxey said:
Impose away. Without seeing your code is impossible to say what the issue
might be.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



Elessvie said:
Thank you very much for your response, Greg.

Your code is what I have, too (except inside the Find loop I use
.Text = "[^0034^0147]*[^0034^0148]"
instead of
.Text = """<*>""").

But I have the find loop being called from a button in a UserForm. The
button passes along a font attribute choice and the code uses Case
statements
to assign that attribute to the found text, then continues looping through
the document.

It works great until it comes upon a table. Then, if the found text is
inside a cell, the loop just goes round and round inside that cell.

I haven't posted my code because I didn't want to impose on you. It looks
like I'm not coding properly for instances of Tables. I was hoping there
was
some information about how tables are handled during a Find, but I can't
find
any.

Am I on the right track, in thinking tables are the problem?

Thank you for whatever help you can offer.

-Lynne



Greg Maxey said:
Try:

Sub FindAndBoldTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = """<*>"""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub


Elessvie wrote:
Hello, people!

With much help from you kind folks, I've created a macro that searches
through a document and replaces text within quotations with text that
is bold.

This works great in documents without tables. But if a document has
tables sprinkled throughout, and a table cell has text within
quotations, the macro hangs there. I've watched with Step-Into, and
the macro never seems to exit the cell.

Are tables to be handled differently from documents? Any suggestions
(including pointing me to information on this) would be greatly
appreciated!

Thank you,

-Lynne
 

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