View Single Post
 
Old 07-30-2018, 01:51 PM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

As usual word can deliver most of what we want but not all.

The advice I provided above works BUT is not satisfactory as it is not possible to separate the label text from what appears in the caption.

E.g. if you define a caption with Label 'Figure2' then what appears in the caption is 'Figure2' so you will get

Figure2 1: Caption text blah blah blah

So as I appear to have misled you please find below a macro that will go through your dissertationand insert the correct styleref in any Table/Figure/Equation caption. Word then correctly reproduces the additional numbering in cross references and TOC.

Code:
Sub update_captions()

Dim my_field                                    As Word.Field

    
    For Each my_field In ActiveDocument.Fields
        If my_field.Type = wdFieldSequence Then
            insert_previous_heading_number my_field
        End If
    Next
    
End Sub

Sub insert_previous_heading_number(this_field As Word.Field)

Const styleref_text                             As String = "Styleref ""XX""  \r "
Dim styleref_range                              As Word.range

    Set styleref_range = this_field.result
    
    With this_field.Code
        If InStr(.Text, "Table") = 0 And InStr(.Text, "Figure") = 0 And InStr(.Text, "Equation") = 0 Then
            Exit Sub ' DO nothing
            
        End If
        
    End With
    
    If this_field.result.Previous(unit:=wdCharacter).Text = "-" Then
        styleref_range.MoveStart Count:=-2
        styleref_range.Fields(1).Delete
        styleref_range.Collapse Direction:=wdCollapseStart
    Else
        styleref_range.Move Count:=-1
        styleref_range.InsertBefore Text:="-"
        styleref_range.Collapse Direction:=wdCollapseStart
    End If
    
    styleref_range.Fields.Add _
            range:=styleref_range, _
            Type:=wdFieldEmpty, _
            Text:=replace(styleref_text, "XX", CStr(get_previous_heading_level(this_field.result))), _
            PreserveFormatting:=False

End Sub

Function get_previous_heading_level(ByVal this_range As Word.range) As Long

    Do While this_range.ParagraphFormat.OutlineLevel = wdOutlineLevelBodyText
        Set this_range = this_range.Previous(unit:=wdParagraph)
    Loop
    
    get_previous_heading_level = this_range.ParagraphFormat.OutlineLevel
End Function
After running the macro please be sure to select the whole document and update all to make sure that all cross references and TOC's are updated correctly.

And of course, please test the macro on a COPY of your dissertation.
Reply With Quote