WORD PROGRAMMING (2003,SP3, on XP):
Before I layout my problem, first go Add 3 New Styles to a blank doc: Style1 (based on Heading 1), Style2 (based on Heading 2), Style3 (based on Heading 3). Now type a dummy Para of EACH type (include all 3 of the new Styles: Style1-3) on page1. Then put cursor at very top of page on a Normal style para.
Now start recording a Macro:
With the recorder going, build a TOC via the normal "Insert/Reference/Index and Tables" technique. Pick 'Template', set Outline Levels to 2, go to Options and UNCHECK the 3 Heading selections and then CHECK Style1 and Style2, but leave Style3 UNCHECKED! So, all you have checked is Style1 & 2.
Now "OK" all the way out the of TOC dialog, and notice that the TOC is exactly as you want it. If it gives you an error this 1st time, rebuild overlaying the TOC line (with the recorder still running) making sure you uncheck the Heading styles and check only Style1 and Style2.
On the 2nd attempt, it now should appear correctly (only giving you the 2 Styles in the TOC).
Now, select the entire TOC, and RUN your new macro. Instead of giving you just Style1-2, your new TOC will INCLUDE Style3. That's the problem!
Look at your Macro (read up about VB details on TOC), and you WON'T find a good reason why your macro shouldn't work.
Lastly, on a correctly built TOC (the non-VB way), toggle to the FIELD CODES and write those down. Then run your macro (giving the unwanted Style3). Then change the FIELD CODES to match the correct look. It doesn't work any better. If fact, it looks kind of corrupted. So, this backdoor method doesn't work either.
Any Word Programming GURUS know how to make the macro ONLY result in showing TWO User Defined OUTLINE LEVEL Styles (level 1 & 2 ONLY)??
Sub Macro1() 'TOC insert code as recorded UNCHANGED. 'This doesn't work; results in giving Style1, Style2 AND the unwanted STYLE3 With ActiveDocument .TablesOfContents(1).Delete .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _ True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _ :="Style1,1,Style2,2", UseHyperlinks:=True, HidePageNumbersInWeb:= _ True, UseOutlineLevels:=False .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndent End With End Sub Sub Macro2() 'Changed UseOutlineLevels:=F to True ... Makes NO DIFF!! 'Same result ... gives Style1, Style2 AND the unwanted STYLE3 With ActiveDocument .TablesOfContents(1).Delete .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _ True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _ :="Style1,1,Style2,2", UseHyperlinks:=True, HidePageNumbersInWeb:= _ True, UseOutlineLevels:=True .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndent End With End Sub Sub Macro3() 'Tried inserting the string text: [,"",3] inside the AddedStyles 'string, thinking this might FORCE it to BLANK OUT STYLE3. Oddly, it does 'NOT ERROR, but makes NO DIFF. ... Still includes the unwanted STYLE3!! With ActiveDocument .TablesOfContents(1).Delete .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _ True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _ :="Style1,1,Style2,2,"",3", UseHyperlinks:=True, HidePageNumbersInWeb:= _ True, UseOutlineLevels:=True .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndent End With End Sub
Last edited by sauerj; 09-24-2010 at 11:14 AM.
Once I tried various uses of the other TOC.Add arguments, I hit upon the solution.
Yet another case where the recorder didn't result in correct working code.
Sub Macro4() 'Recorder didn't provide correct code. Once I added the two 'UpperHeadingLevel and LowerHeadingLevel limits (1 & 2), then it worked With ActiveDocument .TablesOfContents(1).Delete .TablesOfContents.Add Range:=NewTOC, RightAlignPageNumbers:=True, _ UseHeadingStyles:=False, UpperHeadingLevel:=1, LowerHeadingLevel:=2, _ IncludePageNumbers:=True, HidePageNumbersInWeb:=True, _ AddedStyles:="NarrNum_L2_1.x,2,NarrNum_L6_1.2x,2,NarrNum_L1_x.0,1", _ UseHyperlinks:=False .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndent End With End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks