Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2016, 10:24 AM
amer1749 amer1749 is offline Code to creating a custom TOC including too many styles Windows 7 64bit Code to creating a custom TOC including too many styles Office 2013
Novice
Code to creating a custom TOC including too many styles
 
Join Date: Jun 2016
Posts: 5
amer1749 is on a distinguished road
Default 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
Despite code specifying exactly what styles to use, the resulting TOC uses Doc Title, all nine Heading N styles, and the custom TOC style -- which results in the TOC displaying an entry for itself, which is just stupid.

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"
. The MSDN reference for the TableOfContents.Add method does not show the levels, just a comma-delimited list of styles to include. Doesn't matter; the code fails the same way.)
Reply With Quote
  #2  
Old 06-30-2016, 09:28 PM
Guessed's Avatar
Guessed Guessed is offline Code to creating a custom TOC including too many styles Windows 10 Code to creating a custom TOC including too many styles Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 07-01-2016, 10:18 AM
amer1749 amer1749 is offline Code to creating a custom TOC including too many styles Windows 7 64bit Code to creating a custom TOC including too many styles Office 2013
Novice
Code to creating a custom TOC including too many styles
 
Join Date: Jun 2016
Posts: 5
amer1749 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 07-01-2016, 04:11 PM
Guessed's Avatar
Guessed Guessed is offline Code to creating a custom TOC including too many styles Windows 10 Code to creating a custom TOC including too many styles Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 07-03-2016, 10:42 AM
amer1749 amer1749 is offline Code to creating a custom TOC including too many styles Windows 7 64bit Code to creating a custom TOC including too many styles Office 2013
Novice
Code to creating a custom TOC including too many styles
 
Join Date: Jun 2016
Posts: 5
amer1749 is on a distinguished road
Default 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"?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to creating a custom TOC including too many styles 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
Code to creating a custom TOC including too many styles Creating Styles Template? Stone Word 2 09-11-2012 04:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:07 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft