PDA

View Full Version : [SOLVED:] Finding and replacing a SEQ field



Roderick
09-19-2018, 04:01 PM
I'm getting into an awful muddle in trying to find a series of SEQ fields formatted with a particular style.


Dim strSearch As String
Dim strReplace As String
Dim strSEQtype As String
Dim objRange As Range, objRangeNext As Range, objRange3 As Range

ActiveWindow.View.ShowFieldCodes = True

strSEQtype = "Table"

' restart sequence after each instance of custom style
Set objRange = ActiveDocument.Range
With objRange.Find
.Format = True
.Style = ActiveDocument.Styles("Caption Tables")
While .Execute
Set objRangeNext = objRange.Duplicate
objRangeNext.Collapse wdCollapseEnd
objRangeNext.End = ActiveDocument.Range.End
With objRangeNext.Find
.Format = True
.Style = ActiveDocument.Styles("Caption Tables")
If .Execute Then
objRange.End = objRangeNext.Start - 1
Else
objRange.End = ActiveDocument.Range.End
End If

' At this point, objRange covers the range between two
' instances of strCustStyle, or the range following
' the last instance. Now look for the first SEQ field
' within that range.
Set objRange3 = objRange.Duplicate
With objRange3.Find
.Text = "Seq " & strSEQtype & " \* ARABIC"
If .Execute Then
If objRange3.InRange(objRange) Then
objRange3.Text = "Seq " & strSEQtype & " \* ARABIC \r 1"
End If
End If
End With
End With
objRange.Collapse wdCollapseEnd
Wend
End With

As you can see, the field I want to find has the string "SEQ Table \* ARABIC" and I want to replace it with "SEQ Table \* ARABIC \r 1"

When I run the procedure it goes into circular mode and I have to force it to stop.

Could anyone suggest please, how I could simplify the code so that it does replace the field?

Thanks for any help.

Roderick

macropod
09-19-2018, 10:47 PM
You don't actually need a macro for this. Simply press Alt-F9 to expose field codes in the document then use Find/Replace with:
Find = SEQ Table \* ARABIC
Replace = SEQ Table \* ARABIC \r 1
Then press Alt-F9 to hide field codes in the document, followed by Ctrl-A, F9 to update them.

gmaxey
09-21-2018, 08:22 AM
If I understand correctly, you want to reset your Seq Tables field after each instance of Caption Tables style:


Sub ScratchMfacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/21/2018
Dim strSEQtype As String
Dim oRng As Range
Dim oRngSegment As Range
ActiveWindow.View.ShowFieldCodes = True
strSEQtype = "Tables"
Set oRng = ActiveDocument.Range
With oRng.Find
.Format = True
.Style = ActiveDocument.Styles("Caption Table")
While .Execute
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.Range.End
Set oRngSegment = oRng.Duplicate
With oRngSegment.Find
.Text = "Seq " & strSEQtype & " \* ARABIC"
If .Execute Then
If oRngSegment.InRange(oRng) Then
oRngSegment.Text = "Seq " & strSEQtype & " \* ARABIC \r 1"
End If
End If
End With
Wend
End With
lbl_Exit:
Exit Sub
End Sub