0
votes

My word document has styles created, for example My_Style (which i use for titles). I want to create entries to illustrations that get the correlative number from that style.

I have this code:

Sub Macro4()
'
' Macro4 Macro
'
'
    With CaptionLabels("Illustration")
        .NumberStyle = wdCaptionNumberStyleArabic
        .IncludeChapterNumber = True
        .ChapterStyleLevel = 2
        .Separator = wdSeparatorHyphen
    End With
    Selection.Style = ActiveDocument.Styles("My_Style")
    Selection.InsertCaption Label:="Ilustración", TitleAutoText:= _
        "InsertTitle4", Title:="", Position:=wdCaptionPositionAbove, _
        ExcludeLabel:=0
End Sub

The result says Illustration¡Error!.

I think the error is that the macro iscreating the entries with the Style Title1 from word but i dont have any text with that style because i am using my own created styles.

I don't know where can i especify that i want to use my style to create the entries.

Thank you.

1
Basically, you should specify a range and apply a style to that range. Your code Selection.Style = ActiveDocument.Styles("My_Style") does that. Whatever relationship the code before and after this line has on your intention isn't clear. I think you will make progress faster if you start with declaring a range and then manipulate that range. For example, Dim Rng As Range and Set Rng = Selection.Range. Perhaps, next you may want to add the caption in that range, perhaps at its end, then format the caption, and apply the style to it.Variatus

1 Answers

0
votes

What you are trying to do is not possible. Word can only include the number from built-in heading styles in captions.

This is just one of the reasons why you should use the built-in heading styles as Shauna Kelly explains here.