PDA

View Full Version : Solved: code to create table of contents



eed
02-09-2006, 02:44 PM
Hi, all,

Let me see if I can explain this clearly. I want my code to create a Table of Contents including all items in a style I've defined, SectionTitle, but I do NOT want the TOC to include any of the standard header styles. It works correctly when I do it manually, but I can't seem to make this work correctly from code.

In my Word document, I manually created a TOC including Header 1 and my SectionTitle style. Then I manually created a TOC excluding Header 1 and including only my SectionTitle style. I used the Macro Recorder to trap and compare the code:

'TOC includes Header1 AND SectionTitle styles
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=1, IncludePageNumbers:=True, AddedStyles:= _
"SectionTitle,1", UseHyperlinks:=True, HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With

'TOC includes ONLY SectionTitle styles
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
:="SectionTitle,1", UseHyperlinks:=True, HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With

I tried using the second piece of code (I'm working from Access VBA), but the resulting TOC still includes text formatted with the Header1 style as well as the SectionText style. I can't determine what Word is interpreting during manual creation that I'm not managing to tell it during automated creation.

I'd appreciate any insights. Please let me know if I haven't explained my problem clearly enough.

Thanks!!!
~eed

XLGibbs
02-12-2006, 10:56 AM
Hmm. Perhaps you could create a new Header style and refer to that instead...I have played with this, and can't seem to find where the error lies, since the recorded action performing correctly subsequently produces the wrong result...seems odd that this would happen...

TonyJollans
02-12-2006, 12:28 PM
This looks like a bug to me. I just tried a simple ... ActiveDocument.TablesOfContents.Add Range:=Selection.Range, UseHeadingStyles:=False and got heading styles. I tried playing with a few options but couldn't stop them being generated.

A different approach, as a work around, seems to work ... ActiveDocument.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldTOC, _
Text:="\h \z \t ""SectionTitle,2""", _
PreserveFormatting:=False

eed
02-14-2006, 08:53 AM
Hi, Tony,

Glad to know someone else could reproduce the problem; I was beginning to think I was just crazy. I will admit that I'm a little muddy on exactly what your snippet of work-around code is doing... but it works! A ToC without the Heading1 style; at last! I'm much obliged for the help.

However, I am now butting my head up against a second, related problem. My document is divided into a variety of sections, and each section is paginated separately, so you get pages 1-1, 1-2, 1-3, 2-1, 2-2, 3-1,... and so forth. I have a chapter number inserted at the beginning of each section and the page numbers are formatted to include the chapter number before the page number and to restart the page numbering in each section at 1.

This works just fine in the page footers themselves. I successfully get 1-1, 1-2, 2-1, etc. But then when I create a Table of Contents, the ToC shows EVERYTHING as being on page 1-1.

If I'm not explaining the problem well enough or if you need to see the code (since I am most likely mucking something up), I'd be happy to post an abridged version of my project, if it would help someone tell me where I'm off-base...

Thanks!!!!
~ eed

TonyJollans
02-15-2006, 01:19 PM
Sorry, eed, I haven't forgotten you - just been a bit preoccupied.

I can't replicate this quickly - if you have a sample document you could post it would help a lot.

eed
02-16-2006, 11:37 AM
Tony,

Thanks, I finally figured it out. It had something to do with how the options were set for inserting page numbers in each section... I don't know. It seemed strange to me, so it's hard for me to explain, but I got it sorted out somehow!

Thanks for the help, it's GREATLY appreciated, :thumb
eed

TonyJollans
02-16-2006, 12:18 PM
Glad you've got it sorted - happy I could help.