nafarkash
nafarkash

Reputation: 359

MS Word: Create Table of Figures with two SEQIdentifiers in it via VBA

My goal is to create a TOC with two SEQIdentifiers in it.
It is described and answered HERE, though the given answer is manually configured, and I want to activate it with a macro.

Brief description
I have a sequential Figures throughout the document which can be gathered with Table of figures {SEQ \c "Figure"}.
The Figure structure is as follows:
Figure {STYLEREF 1 \s}-{SEQ Figure \*Arabic \s 1} - Result with 'Figure 1-1' for example.

The client request is to add "Point Figure", meaning between two figures: Figure 1-1 and Figure 1-2 the client can add Figure 1-1.A, Figure 1-1.B and so on.
Here is how I've initially created the sturcture:
Figure {STYLEREF 1 \s}-{SEQ Figure \*Arabic \c}.{SEQ PointFigure \* Alphabetic \s 1}.

The problem now is that I can not include both of them in a single Table of Figures.

Trying to implement the given answer:
So, my next approach was starting to implement the answer given in the link above.
The given answer by the way is as follow:

Here is my code followed by explanation and my problem:

Sub createPointFigure()
Dim rng As Range
Dim fld As Field
Dim searchText As String

Set rng = Selection.Range
rng.InsertAfter "Figure "
rng.Collapse wdCollapseEnd
Set fld = rng.Fields.Add(rng, wdFieldEmpty, "StyleRef 1 \s", False)
Set rng = fld.result
'Move focus after the inserted field
rng.Collapse wdCollapseEnd
rng.MoveStart wdCharacter, 1
rng.InsertAfter "-"
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, wdFieldEmpty, "SEQ Figure \c", False

' select the entire inserted text
Selection.MoveRight wdWord, 4, wdExtend
searchText = Selection.Text
Set rng = Selection.Range

' Search for the specific figure in text
Selection.Collapse wdCollapseStart
Dim found As Boolean
found = False
While Not found And Selection.Start <> 1
    findText searchText, False
    For Each fld In Selection.Fields
        If fld.Type = wdFieldSequence Then
            ' look for the original seq field
            If InStr(1, fld.Code.Text, "\s 1", vbTextCompare) Then
                found = True
                Exit For
            End If
        End If
    Next fld
    If found Then
        ActiveDocument.Bookmarks.Add Selection.Text, Selection
    Else
        ' Collapse to the beginning and keep looking for the next one
        Selection.Collapse wdCollapseStart
    End If
Wend
End Sub

The findText method:

Sub findText(searchParam As String, forwardDirection)
With Selection.find
    .ClearFormatting
    .Text = searchParam
    .Forward = forwardDirection
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
End With
End Sub

Explanation:

Problems

Thanks.

Upvotes: 2

Views: 958

Answers (1)

nafarkash
nafarkash

Reputation: 359

Thanks to @CindyMeister guidance, here is an elegant answer for my problem.

Point Figure configuration:
Figure {STYLEREF 1 \s}-{SEQ Figure \c}.{SEQ PointFigure \* Alphabetic \s 1}. Figure Text *Style Separator* {TC "{STYLEREF "Figure Title"}" \f F}

Table of Figures Configuration:
{TOC \f F \c "Figure"}

Remarks:

  • Figure style in my example is configured as "Figure Title"
  • The {TC} must be of a different style in order for STYLEREF to work.
    For that I've used Style Separator (Ctrl + Alt + Return). Character style is another option I think.
  • All {} brackets in the code examples are Word Fields (Ctrl + F9)
  • I inserted the Point Figure text as an AutoText, which is added via Macro.
  • In order to achieve unique point numbering for each 'Figure 1-1' text, I've added a reset field before each one: {SEQ PointFigure \h \r 0}

Upvotes: 3

Related Questions