0
votes

I would like to change all captions from one type to another. While not modifying any other filed codes accidentily.

The process I've come up with is two step: First changing the text inside the field code and the text before it.

i.e. from

Table { SEQ Table * ARABIC }

to

Figure { SEQ Figure * ARABIC }

I have experimented with manual editing and changing the type inside does not automaticaly change the label outside of the field.

To change the text I use Find/Replace, which works fine:

With Selection.Find
    .Style = ActiveDocument.Styles("Caption")
    .Text = "Figure"
    .Replacement.Text = "Table"
    .Forward = True
    .Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

To do the field codes themslves I'm having issues. The following code will perform the change:

Dim rngTemp As Range
Set rngTemp = ActiveDocument.Fields(1).Code
rngTemp.Text = " SEQ Figure \* ARABIC "
ActiveDocument.Fields(1).Update

however I can't make sure it only changes a specific type of field, i.e. captions. As it is the code changes any field based on its index number.

I'm also having trouble getting it to loop with a for/each replaceing the number with an i. I get an error saying "object does not suppor this method"

Any help would we wonderfull thank you.

2
For caption iteration, look at thisScott Holtzman

2 Answers

1
votes

Based on link above, how about the below code (Untested)

Dim oField As Field
Dim sCode As String
Dim bFoundOne As String

For Each oField In ActiveDocument.Fields

    If oField.Type = wdFieldSequence Then

        sCode = oField.Code

        If InStr(sCode, "Table") <> 0 Then oField.Code = Replace(sCode, "Table", "Figure")

    End If
Next
0
votes

Thank you Scott, that was a great help!

There was only on item missing, in the replace part of the code .Text is needed. Yet your solution of just replacing one word rather than the whole text is much more elegent.

If InStr(sCode, "Table") <> 0 Then oField.Code.**Text** = Replace(sCode, "Table", "Figure")

I've tried to make the code adaptable, by taking the find/replace text out and into a string. Added the Find/replace code for the preceding text in there. And stuck in a counter for good measure.

Dim oField As Field
Dim sCode As String
Dim TypeFind As String
Dim TypeReplace As String
Dim bFoundOne As String


bFoundOne = 0

'Swap strings around as required
TypeReplace = "Table"
TypeFind = "Figure"
' = "Equation"

'-- Change Field Code --
For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldSequence Then
        sCode = oField.Code
        If InStr(sCode, TypeFind) <> 0 Then
            bFoundOne = bFoundOne + 1 'counting how many
            oField.Code.Text = Replace(sCode, TypeFind, TypeReplace)
        End If
    End If
Next

'-- Change preceding text --
With Selection.Find
    .Style = ActiveDocument.Styles("Caption")
    .Text = TypeFind
    .Replacement.Text = TypeReplace
    .Forward = True
    .Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

MsgBox ("Changed: " & bFoundOne) 'show how many when finished