Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross reference to custom reference type

Tags:

ms-word

vba

I'm trying to add a cross reference into a SEQ field.
My document contains "point headings" which means that between two heading elements, the user can add an extension (between 1.1 and 1.2 may be 1.1A, 1.1B, ...)

Here is how the point heading code looks like:
{STYLEREF "HEADING 2" \N}{SEQ "HEADING 2 POINT" \* ALPHABETIC \S 2}
Which results with: 1.1A

I want to be able to do a cross reference into the point heading.
While I can set the reference type into 'Heading' I can't find out how to reference it to a custom element.

Searching through the web did not reveal any solution but some clues that it might be possible:

  • This website which explains cross-reference formatting, contains an image with custom type (My New Caption).
  • Microsoft DOC's description for ReferenceType is: The type of item for which a cross-reference is to be inserted. Can be any WdReferenceType or WdCaptionLabelID constant or a user defined caption label.

My client is used to work with the cross reference dialog box hence I prefer this approach, but VBA script will also be appreciated.

Thanks!

Update:

I'll try to describe my constraints and environment.
Headings 1-9 are used inside Multi-Level list item, hence they have custom styling.
They cannot be changed.

For a specific task, which is described and answered here, I've created what I call 'Point Headings'.
'Point Headings' are basically an extension that the user can add in between the Multi-Level numbering with a VBA macro.
Let's say that I have two Heading 2 items (1.1, 1.2), the user can add 1.1A, followed by 1.1B and so on.
The user can add point headings from level 2 up to level 5.
Their style is 'Heading 2 Point', 'Heading 3 Point' and so on, and each one is based on its relevant Heading.

As described above, eventually in the document, the word field has the following structure: {STYLEREF "HEADING 2" \N}{SEQ "HEADING 2 POINT" \* ALPHABETIC \S 2}.

My goal is to be able to cross reference into these items, but they do not appear in the Heading type, well because they are not of style Heading.

I wish to be able to create a custom reference type, which will show these items.

like image 456
nafarkash Avatar asked Feb 04 '26 16:02

nafarkash


1 Answers

After some research, here is my answer. Hopefully it will help some future viewers.

Private Sub createPointHeader(pointLevel As Integer, Optional appendixPrefix As String = "")
Dim sQuote As String, referencedStyle As String, captionName As String
sQuote = Chr(34)
referencedStyle = appendixPrefix & "Heading " & pointLevel
captionName = referencedStyle & " Point"
With Selection
    .Fields.Add .Range, wdFieldEmpty, "StyleRef " & sQuote & referencedStyle & sQuote & " \n", False
    .Collapse wdCollapseEnd
    CaptionLabels.Add (captionName)
    .InsertCaption Label:=captionName, ExcludeLabel:=True
    ' Select the created field
    .MoveLeft Count:=1, Extend:=True
    ' Replace the syntax from Arabic to Alphabetic
    .Fields.ToggleShowCodes
    With .find
        .Text = "ARABIC"
        .Forward = False
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
        If .Found = True Then
            Selection.Range.Text = "ALPHABETIC \s " & pointLevel
        End If
    End With

    .Fields.ToggleShowCodes
    .Fields.Update
    .MoveRight Count:=1
    .InsertAfter vbTab
    .Collapse wdCollapseEnd
    ' Apply style after .InsertCaption, because it changes the style to Caption
    .Style = ActiveDocument.Styles(referencedStyle & " Point")
    End With
End Sub

Few remarks

  • I have two styles to base upon: Heading (2-5), and Appendix Heading (2-5). This is the reason for the optional appendixPrefix as a sub variable.
  • CaptionLabels.Add as I've checked can get the same value. No need to check in advance if it already exists.
  • Selection.InsertCaption automatically changes the style into Caption. This is why I apply the style change at the end.

The result

  • Here is how Point Heading 2 looks like:
    {STYLEREF "HEADING 2" \N"}{SEQ HEADING_2_POINT \* ALPHABETIC \S 2}

  • Snapshot of the document with the point headings Point Headings example

  • And finally, as requested, cross reference to the Point headings from the Cross reference box Cross reference dialog box

like image 85
nafarkash Avatar answered Feb 08 '26 18:02

nafarkash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!