I write technical documentation for a small-to-medium-sized software company. I’ve been trying to solve a couple of persistent formatting glitches in my TOCs and indexes, but Word seems determined to frustrate my efforts. I’m just about at my wit’s end, so I’m hoping somebody here can offer a useful suggestion.
The first problem is a familiar one: Jason tabs in my table of contents. I’ve followed all the advice in [link //word.mvps.org/faqs/formatting/TOCJasonTabs.htm]Dave Rado’s MVP article on the subject[/url], defined my heading styles with a macro using named list templates, disabled auto-update for all styles, etc., but Word still insists on tweaking my tab settings whenever I rebuild the TOC. So I’ve written the following macro to fix them:
This seems to work fine when I run it, but still doesn’t solve my Jason tab problem, for reasons explained below.
The second problem affects the index. My document is a reference manual for an application programming interface. The index simply lists the API functions with the page numbers of the sections describing them. So each section heading includes a hidden index marker of the form [tt]{ XE "function_name" }[/tt]. In the index, I want the function names to appear in the distinctive (monospace) computer code font, with the associated page numbers in normal body font. Other word-processing programs (such as FrameMaker) provide ways to specify such formatting information in the index tag, but Word, as far as I know, does not. I’ve tried formatting the index tags themselves with the character style I want applied to them in the index ([tt]cfix[/tt], for “code font in index”), but that format doesn’t get picked up and applied to the resulting index entry. If anyone reading this knows of a way to specify this kind of formatting for index entries, I’d love to know about it; but lacking that, I thought I could manage it with a macro to apply the formatting after building the index:
Now I can intercept the Update Fields command and automatically adjust the formatting for the TOC and index whenever they’re rebuilt:
So far, all of this works just fine: whenever I update fields in my document, if the TOC and/or index are included in the selection their formatting gets repaired just the way I want it to. The problem arises when I go to generate a PDF of my document. As soon as I select the Print command, I see the message [tt]Word[/tt] [tt]is[/tt] [tt]repaginating[/tt] [tt]"API[/tt] [tt]Reference.doc"[/tt] appear in the status bar, followed by [tt]Word[/tt] [tt]is[/tt] [tt]updating[/tt] [tt]the[/tt] [tt]table[/tt] [tt]of[/tt] [tt]contents...[/tt], and then [tt]Word[/tt] [tt]is[/tt] [tt]searching[/tt] [tt]for[/tt] [tt]index[/tt] [tt]entries[/tt] and [tt]Word[/tt] [tt]is[/tt] [tt]building[/tt] [tt]the[/tt] [tt]index.[/tt] These rebuilding operations do not seem to be going through the Update Fields command, and hence are not getting intercepted by my UpdateFields macro. The result is that the damned Jason tabs and incorrect character formatting get put back into my TOC and index before the PDF is generated. I can’t see any way to create a PDF with the document formatted the way I want it. Has anyone got any constructive suggestions?
Thanks in advance for any help you can offer.
The first problem is a familiar one: Jason tabs in my table of contents. I’ve followed all the advice in [link //word.mvps.org/faqs/formatting/TOCJasonTabs.htm]Dave Rado’s MVP article on the subject[/url], defined my heading styles with a macro using named list templates, disabled auto-update for all styles, etc., but Word still insists on tweaking my tab settings whenever I rebuild the TOC. So I’ve written the following macro to fix them:
Code:
Sub FixTOCTabs()
'
' FixTOCTabs macro
' Eliminate spurious tab settings from document table of contents
'
With ActiveDocument.TablesOfContents(1).Range.Paragraphs
.TabStops.ClearAll
.Reset
End With '.ActiveDocument.TablesOfContents(1).Range.Paragraphs
End Sub 'FixTOCTabs
The second problem affects the index. My document is a reference manual for an application programming interface. The index simply lists the API functions with the page numbers of the sections describing them. So each section heading includes a hidden index marker of the form [tt]{ XE "function_name" }[/tt]. In the index, I want the function names to appear in the distinctive (monospace) computer code font, with the associated page numbers in normal body font. Other word-processing programs (such as FrameMaker) provide ways to specify such formatting information in the index tag, but Word, as far as I know, does not. I’ve tried formatting the index tags themselves with the character style I want applied to them in the index ([tt]cfix[/tt], for “code font in index”), but that format doesn’t get picked up and applied to the resulting index entry. If anyone reading this knows of a way to specify this kind of formatting for index entries, I’d love to know about it; but lacking that, I thought I could manage it with a macro to apply the formatting after building the index:
Code:
Sub FixIndexFormat()
'
' FixIndexFormat macro
' Apply correct character formatting to index entries
'
Dim theIndex As Index
Dim indexRange As Range
Dim indexEntries As Paragraphs
Dim indexEntry As Paragraph
Dim entryRange As Range
Dim formatRange As Range
Set theIndex = ActiveDocument.Indexes(1)
Set indexRange = theIndex.Range
Set indexEntries = indexRange.Paragraphs
Set formatRange = theIndex.Range
For Each indexEntry in indexEntries
Set entryRange = indexEntry.Range
With entryRange
formatRange.Start = .Start
With entryRange.Find
.Execute FindText:=vbTab
If .Found Then
formatRange.End = .Parent.Start
StatusBar = Reformatting index entry " & formatRange.Text
formatRange.Style = "cfix"
End If '.Found
End With 'entryRange.Find
End With 'entryRange
Next indexEntry
End Sub 'FixIndexFormat
Code:
Sub UpdateFields()
'
' UpdateFields macro
' Intercept Update Fields command to fix TOC and index formatting
'
Selection.Fields.Update
Dim selRange As Range
Dim tocRange As Range
Dim indexRange As Range
Set selRange = Selection.Range
Set tocRange = ActiveDocument.TablesOfContents(1).Range
If tocRange.InRange(selRange) Or selRange.InRange(tocRange) Then _
Call FixTOCTabs
Set indexRange = ActiveDocument.Indexes(1).Range
If indexRange.InRange(selRange) Or selRange.InRange(indexRange) Then _
Call FixIndexFormat
End Sub 'UpdateFields
Thanks in advance for any help you can offer.