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
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