简体   繁体   中英

Word 2010 Table of contents manipulation using VBA

I'm attempting to change font color of selected lines in the table of contents, based on certain criteria.

I have an array i move through , where each element is a line number in the TOC.

for I = 1 to TOC_INDEX
    Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=TOC_MORT(J) + 1
    selection.expand wdline
    Selection.Font.ColorIndex = wdRed
next

This works perfectly, except for when J is 1, ( the first entry in the TOC ) the entry

selection.expand wdline 

selects the entire TOC... Is there a better method for modifying the TOC, Does anybody have any examples of using the TOC Object

sourceDocument.TablesOfContents().Range.Fields().select

is the solution I found

In my case each line in the TOC so my code looks like this:

For I = 1 To TOC_INDEX
    Debug.Print TOC_INDEX, I, TOC(I)
    If I = 1 Then
        sourceDocument.TablesOfContents(1).Range.Fields(TOC(I) + 1).Select
    Else
        sourceDocument.TablesOfContents(1).Range.Fields(TOC(I * 2) + 4).Select
    End If
    Selection.Font.ColorIndex = wdRed
Next

My task was to highlight entries in the TOC that met certain conditions. (In my case it was the last modification date of a specific document). This data was entered into an array prior to invoking this code. The entire document consisted of a list of other documents and their table of contents, and hyperlinks to those chapters.

Now, when readers view the TOC, they know at a glance that specific documents sections should be viewed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM