2
votes

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:

  • Bookmark the seq field with a special name - in the example it's tablea
  • refer to the reference by { SEQ Table \r { REF tablea } }

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:

  • Temporary create the closest Figure text
  • Search backward until finding the appropriate figure (keep looking if found a sequence field with \c).
  • Once found, create a new bookmark with the name
  • Construct the field as the answer suggests (Not implemented in the code)

Problems

  • Testing fails in the insert bookmark line:
    ActiveDocument.Bookmarks.Add Selection.Text, Selection
    Apparently, Bookmark cannot contain numbers and symbols in it.
    How can I distinguish a reusable bookmark? For the next time I'll Create this Figure structure, I would like to reuse the same Bookmark.
  • All this work has huge overhead. Is there a simpler solution to accomplish my goal?

Thanks.

1
Bookmark names must be unique in a document. Bookmark names may not begin with a number and may not contain certain characters. You might look at using TC fields in combination with the SEQ fields.Cindy Meister

1 Answers

3
votes

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}