Stuck In Ye Olde Table Celle -- Thank you for any help you could o

E

Elessvie

Hello again, folks,

On 8/4 I posted a question about a find/replace loop that hanging inside a
table cell. Greg Maxey kindly responded with a sub to loop through a
document. I returned to my macro thinking I’d found my solution. But after
hours of futile bushwhacking, I humbly return to the discussion group, asking
once again if someone can figure out why the macro never gets beyond the
first cell in the first table it comes across.

Our documents are mostly text, sometimes with a table or 2 sprinkled
throughout. The macro has been working just fine as long as there are no
tables. But if a find happens to be inside a cell in a table, the search
range never leaves the cell. I’ve followed it with Step-Into, and it goes
round and round inside that same cell, never leaving, never resetting.

Well, below is my Rube Goldberg-esque code. We have Word 2007, if that
makes a difference.

Thank you to anyone who could guide me.

Sincerely,
-Lynne

Please note that users have a choice of changing the font attribute to bold,
underlined, or italics, but for brevity I’ve included here only bold as a
choice in the user forms.

There is one module (DefinedTermsMacro) and for this example there would be
2 user forms, DefTermsMain and DefTermsContinueBold.

Listed below is first the module code and after that are the user form and
button codes.

The user starts with the StartingSub, pulling up the main user form,
DefTermsMain. From this form the user chooses whether to have the macro just
go through the whole document on its own, or to have the macro pause after
each find/replaced term with a choice to continue or not.

‘==================================================================
Option Explicit
‘==================================================================
Sub StartingSub()
DefTermsMain.Show
End Sub
‘==================================================================
Sub SearchWholeDoc(ByVal fntAttrib As String)
Dim oRngForSearch As Range
Dim oRngOfTerm As Range
Dim oFixCommaRng As Range
Dim oFixPeriodRng As Range
Dim oRngMarkEnd As Range
‘--------------------
'Set search ranges
‘--------------------
Set oRngForSearch = ActiveDocument.Range
Set oRngOfTerm = oRngForSearch.Duplicate
‘--------------------
'If you have returned to find/replace Term-By-Term
‘after pressing “Continue†Button on UserForm,
‘check to see if “Left_Off_Here†exists, and set the new
‘replacement range:
‘--------------------
If ActiveDocument.Bookmarks.Exists("Left_Off_Here") Then
oRngOfTerm.Start =
ActiveDocument.Bookmarks("Left_Off_Here").Range.Start
oRngOfTerm.End = oRngOfTerm.End
‘--------------------
‘Delete bookmark here so it can be recreated later:
‘--------------------
ActiveDocument.Bookmarks("Left_Off_Here").Delete
End If
‘--------------------
'Loop to find all Defined Terms
‘--------------------
Do
With oRngOfTerm.Find
.ClearFormatting
.Text = "[^0034^0147]*[^0034^0148]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
‘--------------------
'If no terms found, exit the loop
‘--------------------
If Not oRngOfTerm.Find.Found Then
Exit Do
End If

With oRngOfTerm
'first shrink oRngOfTerm to be inside of quotes
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
'now bold, underline, or italicize the term
Select Case (fntAttrib)
Case "Bold"
.Font.Bold = True
Case "Underline"
.Font.Underline = wdUnderlineSingle
Case "Italic"
.Font.Italic = True
End Select
End With
‘--------------------
‘Upon reaching the end of the loop, either
'Set a signal if it’s the first time through using “Term-By-Term†Button
‘--------------------
If ActiveDocument.Bookmarks.Exists("Term_By_Term_Signal") Then
Set oRngMarkEnd = oRngOfTerm.Duplicate
With oRngMarkEnd
.MoveEnd Unit:=wdCharacter, Count:=2
.Collapse wdCollapseEnd
.Bookmarks.Add Name:="Left_Off_Here"
End With
Exit Do
Else
‘--------------------
‘Or if it’s looping through the whole document at once,
'reset oRngOfTerm to start after the term just worked on
‘--------------------
oRngOfTerm.Start = oRngOfTerm.End
oRngOfTerm.End = oRngForSearch.End
End If
Loop Until Not oRngOfTerm.Find.Found
End Sub
‘==================================================================
Sub Cleanup()
If ActiveDocument.Bookmarks.Exists("Left_Off_Here") Then
ActiveDocument.Bookmarks("Left_Off_Here").Delete
End If
If ActiveDocument.Bookmarks.Exists("Last_Find") Then
ActiveDocument.Bookmarks("Last_Find").Delete
End If
If ActiveDocument.Bookmarks.Exists("Term_By_Term_Signal") Then
ActiveDocument.Bookmarks("Term_By_Term_Signal").Delete
End If
End Sub
‘==================================================================
Sub TermByTermMarkers()
ActiveDocument.Bookmarks.Add Name:="Term_By_Term_Signal"
End Sub
‘==================================================================
‘==================================================================



‘USERFORMS AND BUTTONS:

‘--------------------
‘MAIN USERFORM
‘(form name = DefTermsMain)
‘--------------------
‘This shows just the code relevant to my problem
‘from the main form.
‘If user clicks this button, the macro will loop through
‘the document, pausing after each term has been bolded,
‘and a Continue?--Yes/No form (code below) is called.
‘--------------------
‘(Option Explicit at top)
Private Sub cmdBWordByWord_Click()
DefTermsMain.Hide
TermByTermMarkers
SearchWholeDoc ("Bold")
DefTermsContinueBold.Show
End Sub
‘--------------------
‘SECONDARY USERFORM FOR TERM-BY-TERM OPTIONS
‘(form name = DefTermsContinueBold)
‘--------------------
Option Explicit
Private Sub cmdContinueBoldYes_Click()
DefTermsContinueBold.Hide
SearchWholeDoc ("Bold")
DefTermsContinueBold.Show
End Sub
Private Sub cmdContinueBoldNo_Click()
DefTermsContinueBold.Hide
Unload DefTermsContinueBold
Unload DefTermsMain
Cleanup
End Sub
Private Sub cmdContinueBoldCancel_Click()
Unload DefTermsMain
Unload DefTermsContinueBold
Cleanup
End Sub
Private Sub DefTermsContinueBold_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub
‘--------------------

‘--------------------
‘MAIN USERFORM
‘(form name = DefTermsMain)
‘--------------------
‘This code for the button for
‘having the macro go through
‘entire document without pausing
‘--------------------
Private Sub cmdBWholeDoc_Click()
DefTermsMain.Hide
SearchWholeDoc ("Bold")
End Sub
 
C

Cindy M.

Hi Elessvie,
On 8/4 I posted a question about a find/replace loop that hanging inside a
table cell. Greg Maxey kindly responded with a sub to loop through a
document. I returned to my macro thinking I’d found my solution. But after
hours of futile bushwhacking, I humbly return to the discussion group, asking
once again if someone can figure out why the macro never gets beyond the
first cell in the first table it comes across.

Yes, I am familiar with the problem. The key to whether it happens or not has to
do with the definition of the RANGE that Find is executing on. Within a Table,
you cannot define a Selection (or Range) that extends, for example from the
middle of a row into the next row. If you try, Word extends the selection to
include the entire first and second rows.

The same problem interferes when trying to use Find from within a table. There
are circumstances when it's not a problem; others where it's definitely a
problem. So I tend to always do something like this pseudocode:

Set rngSearch = ActiveDocument.Content
Set rngSearchResult = rngSearch.Duplicate
Do
boolIsFound = rngSearchResult.Find.Execute('parameters here)
If boolIsFound Then
If .WithinTable Then
'Do whatever
'Loop through all the table cells,
'running Find in each cell's Range
'until the entire table is processed
'then continue a normal Find on the rest of the document
Else
'Do whatever
'continue the find normally
End If
End If
Loop Until not boolIsFound

As you can see, this can get quite complex, which is why I can't just give you a
code sample. I usually break this down into three or four procedures (to avoid
duplicating code).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
E

Elessvie

Thank you so much, Cindy!

I thought I was not seeing some kind of logic mistake on my part, and I got
bleary-eyed trying different approaches to find.

I'd just started looking at ".WithinTable", but it just seemed like one more
klugey thing to add to the macro. Now that I see your answer, it looks like
I was on the right track.

A grateful poster,
-Lynne
 
G

Greg Maxey

Thank you so much, Cindy!  

I thought I was not seeing some kind of logic mistake on my part, and I got
bleary-eyed trying different approaches to find.

I'd just started looking at ".WithinTable", but it just seemed like one more
klugey thing to add to the macro.  Now that I see your answer, it lookslike
I was on the right track.  

A grateful poster,
-Lynne











- Show quoted text -

Cindy, Elessvie,

What is .WithinTable in your line If .WithinTable Then?

Selecting it and pressing F1 results on Keyword not found.
Attempting to run code using that statement returns Compile Error
Method or Data Member not found.

Is is some sort of abbreviated If Selection.Information(wdWithInTable)
Then ?

If so how do you make it work?
 
C

Cindy M.

Hi Greg,
What is .WithinTable in your line If .WithinTable Then?

Selecting it and pressing F1 results on Keyword not found.
Attempting to run code using that statement returns Compile Error
Method or Data Member not found.

Is is some sort of abbreviated If Selection.Information(wdWithInTable)
Then ?

If so how do you make it work?

Ooops, sorry. I was running various scenarios through my head and
originally had a With rngSearchResult in there. Yes, that would be
rngSearchResult.Information(wdWithinTable)

(Obviously, my brain was very close to burn-out when I wrote that
answer!)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

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