Consulting

Results 1 to 7 of 7

Thread: Solved: code to create table of contents

  1. #1
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location

    Solved: code to create table of contents

    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:

    [VBA] '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[/VBA]

    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
    With program specs this fickle, you've just got to believe in Discord.

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    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...
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    This looks like a bug to me. I just tried a simple ... [VBA]ActiveDocument.TablesOfContents.Add Range:=Selection.Range, UseHeadingStyles:=False[/VBA] 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 ... [VBA]ActiveDocument.Fields.Add _
    Range:=Selection.Range, _
    Type:=wdFieldTOC, _
    Text:="\h \z \t ""SectionTitle,2""", _
    PreserveFormatting:=False[/VBA]
    Last edited by TonyJollans; 02-12-2006 at 12:29 PM. Reason: Reformat
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    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
    With program specs this fickle, you've just got to believe in Discord.

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    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,
    eed
    With program specs this fickle, you've just got to believe in Discord.

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Glad you've got it sorted - happy I could help.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •