0
votes

I have created a table of contents (TOC) to my Microsoft Office Word 2013 document using different types of headings linked to the TOC. When I try to modify my TOC 1 style so that only the text inside that TOC paragraph would be bolded, it makes the whole paragraph bolded. What I want to achieve is this:

ONLY THE TEXT HERE IS BOLDED................................1

rather than bolding the dot leader lines nor the page number like this

THE WHOLE PARAGRAPH IS BOLDED.........................1

I can do this manually, but everytime I update my TOC, all these changes go away (everything is bolded/not bolded again). How can I bold only only the text (not dot leaders or page number) inside TOC-style?

1

1 Answers

1
votes

Fields throw away edits when updated - there's little you can do to change this. There is a * MergeFormat switch that can be added to field codes in order to retain formatting. The major drawback, however, is that it "remembers" formatting by character location. So if the text changes (gets shorter or longer) the formatting "goes crazy". Therefore, I don't think that will help you.

So all you can really do is re-apply the formatting after each TOC update. The fastest way to do that is using code (a macro, for example).

Here's a macro that works in a quick test on my system. It searches for each TAB character in the TableOfContents. Then, based on the additional information from the OP that only the second tab should be recognized, uses a second range to check whether the position AFTER the tab is within one centimeter of the right margin.

If that's the case, it repositions the find-target Range from that point back towards the beginning of the paragraph, then applies the formatting.

Note 1: You can change the "cushion" used to determine the distance to the right margin by changing the formula assigned to PageNumLimit.

Note 2: I defined a STYLE named Toc1_Text and use that, rather than formatting with BOLD directly. Doesn't really matter, but it feels "cleaner" to me than direct formatting :-)

Sub FormatTextInTOC()
  Dim rngFind As word.Range, rngFound As word.Range
  Dim bFound As Boolean
  Dim toc As word.TableOfContents
  Dim infoH As Double, pageNumLimit As Double

  Set toc = ActiveDocument.TablesOfContents(1)
  toc.Update
  Set rngFind = toc.Range
  pageNumLimit = rngFind.Sections(1).PageSetup.RightMargin _
                 - CentimetersToPoints(1)
  With rngFind.Find
    .ClearFormatting
    .Text = vbTab
    .Style = word.WdBuiltinStyle.wdStyleTOC1
    Do
        bFound = .Execute
        If bFound Then
            Set rngFound = rngFind.Duplicate
            'rngFound.Select
            rngFound.Collapse wdCollapseEnd
            infoH = rngFound.Information(_ 
                    wdHorizontalPositionRelativeToTextBoundary)
            If infoH >= pageNumLimit Then
                'rngFind.Select
                rngFind.Collapse wdCollapseStart
                rngFind.MoveStart wdParagraph, -1
                rngFind.Style = "Toc1_Text"
                rngFind.Start = rngFind.Paragraphs(1).Range.End
                'rngFind.Select
            End If
        End If
    Loop While bFound
  End With
End Sub