#1
|
|||
|
|||
Code to creating a custom TOC including too many styles
I'm trying to write code to insert a custom table of contents. The code first writes "Table of Contents" in a new paragraph and sets that paragraph to a style other than Heading 1 or Heading 2 (though it is based on Heading 1).
Then I try this: Code:
With ActiveDocument .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _ True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _ :="Heading 1,Heading 2", UseHyperlinks:=True, _ HidePageNumbersInWeb:=True, UseOutlineLevels:=False .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndent End With What am I missing? (FWIW, if I turn on the macro recorder and insert a TOC that correctly excludes the TOC style, I get exactly what I want, and I get almost exactly the code above. The only difference is that the recorder writes Code:
AddedStyles:="Heading 1, 1, Heading 2, 2" |
#2
|
||||
|
||||
You shouldn't need to set the tableader via the vba. This should be done by the styles already in the document.
You could fiddle with this vba code forever but I would just create a TOC manually with whatever styles and formatting you want and then when you are happy with it - save that TOC as a Quick Part building block. Then you can either insert it by choosing it from the quick parts OR by typing its name and pressing F3 (or Enter), OR recording a macro of inserting that building block.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks, Guessed. I've added the TOC to the TOC gallery in the template, which will work fine. I was just curious about why this code doesn't behave the way it should, but I can let that go.
FYI, we found earlier that when a TOC in inserted from the Quick Parts gallery, it tends to add a *lot* of white space at the bottom of the TOC. Adding it to the TOC gallery instead avoids this problem. Thanks again! I appreciate your input. |
#4
|
||||
|
||||
Your code doesn't work the way it should because it doesn't correspond to the way that Word works.
Looking at each line, this is where you are having problems Line 1 produces a field code that looks like this TOC \o "1-9" \h \z \t "Heading 1,Heading 2" So your Heading 1 and Heading 2 entries appear in the TOC because of the \o "1-9" part. Using the \t switch is superfluous in this case because those two styles are already in there. Also "Heading 1,1,Heading 2,2" is what is supposed to be in the quotes - you have to tell Word what TOC style to assign to each style. Line 2 doesn't align with how tab leaders work. .TablesOfContents(1).TabLeader = wdTabLeaderDots A tab leader is a property of a particular tab in a particular paragraph or style. In the context of an entire table of contents which could contain 9 styles and many paragraphs this line means nothing. Plus the first table of contents in a document may not actually BE the TOC that you just inserted in the first line (if there is another TOC earlier in the doc). Line 3 .TablesOfContents.Format = wdIndexIndent I have no idea of what you expected to happen with this line. wdIndexIndent is a built-in constant with a value of 0. What does it mean to set a "Format = 0" when Format might be typeface/font size/bold/italic/paragraph spacing/paragraph indent etc. And where are you trying to apply this 'formatting'?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Doesn't correspond to the way Word works?
Then why is that exactly what the macro recorder records? And more to the point, what code would correspond to "the way Word works"?
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to create custom table styles? | Pey1 | PowerPoint | 1 | 05-25-2016 02:15 AM |
Styles in a Custom Ribbon | inagalaxyfarfarawry | Word | 5 | 12-15-2015 11:18 AM |
Custom Bar Styles | dbsoccer | Project | 7 | 10-31-2013 07:26 AM |
creating new styles | hannu | Word | 8 | 04-28-2013 11:23 PM |
Creating Styles Template? | Stone | Word | 2 | 09-11-2012 04:54 PM |