This is a macro developed earlier by Graham Mayor for which I'm very grateful and which works superbly well up to a point identified below - but only because I've "messed around" with it to try and achieve a solution.
Here is the macro:
Sub ReplaceFieldsV4()
'Graham Mayor - http://www.gmayor.com - Last updated - 24 Sep 2018
'===================================
'this procedure ensures that all the Standard Numbering SEQ fields have the \s option set
'irrespective of what had been put there earlier.
Dim oFld As Field, oNewFld As Field
Dim oRng As Range, oSeq As Range
Dim figRng As Range, figLvl As Long
ActiveWindow.View.ShowFieldCodes = True
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldSequence Then
If InStr(1, oFld.Code, "Figure") > 0 Then
oFld.Select
'this procedure inserts a Chapter Figure caption based on Word Headings levels
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = Right(figRng.Paragraphs.First.Style, 1)
End With
oFld.Code.Text = Replace(oFld.Code.Text, oFld.Code.Text, "SEQ Figure \* ARABIC \s \* MERGEFORMAT")
oFld.Update
Set oRng = oFld.Code
With Selection
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Text:="-"
.Collapse
.MoveLeft Unit:=wdCharacter, Count:=1
End With
'now adds the StyleRef field
Set oNewFld = ActiveDocument.Fields.Add(Range:=.Range, _
Type:=wdFieldEmpty, _
Text:="StyleRef ""Heading " & figLvl & """ \s", _
PreserveFormatting:=False)
oNewFld.Update
Set oRng = oNewFld
End If
End If
Next oFld
ActiveWindow.View.ShowFieldCodes = False
lbl_Exit:
Set oFld = Nothing
Set oNewFld = Nothing
Set oRng = Nothing
Exit Sub
End Sub
What should happen is that a StyRef field should be inserted before the SEQ field, however, and error kicks in at this position here (in red):
Set oNewFld = ActiveDocument.Fields.Add(Range:=.Range, _
Type:=wdFieldEmpty, _
Text:="StyleRef ""Heading " & figLvl & """ \s", _
PreserveFormatting:=False)
oNewFld.Update
Set oRng = oNewFld
...and this is the error message which appears:
Capture.PNG
I've tried "fiddling" with possible permutations to get the field added but don't seem to have the correct procedure to get it right.
Could the correct code be pointed out me, please, to achieve the process?
Thanks
Roderick