PDA

View Full Version : Cover Sheet and TOC Problems - Weird



jadie
06-05-2006, 05:23 AM
I have a macro that, based on the users selection on a userform will insert up to 30 different files into my document.

I then insert a cover sheet, page down and insert a TOC. This works exactly how I want it to when I put a breakpoint in after the insertion of the files (before the cover sheet and TOC) but when I let it run by itself, it puts the TOC at the top (above the cover sheet).

Has me baffled. Here's the code:

'
' go to top and insert cover sheet
'
Selection.HomeKey Unit:=wdStory
Call InsertFile("Cover Sheet.doc")
Selection.InsertBreak Type:=wdPageBreak
'
' insert table of contents
'
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveUp Unit:=wdScreen, Count:=1
Selection.TypeText Text:="TABLE OF CONTENTS"
Selection.MoveLeft Unit:=wdCharacter, Count:=17, Extend:=wdExtend
Selection.Font.Name = "Garamond"
Selection.Font.Size = 14
Selection.Font.Bold = True
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.TypeParagraph
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=1, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With

fumei
06-05-2006, 07:15 AM
Other than something you have in Sub InsertFile (not posted) I would suspect:Selection.MoveUp Unit:=wdScreen, Count:=1 Why are you moving the Selection around?

And, it is much better if you do not use the Selection at all.

jadie
06-05-2006, 07:30 AM
Thanks for the reply. That did fix it. I was having a problem before where the header of the first inserted file was being manipulated with the TOC heading. I started going down two pages and then back up one which eliminated that problem. I subsequently did something else that also eliminated that problem but forgot to go back and get rid of the two down, one up.

I still don't understand why it worked when I inserted a breakpoint and manually stepped through vs. not working by running it all the way through.

fumei
06-05-2006, 06:25 PM
You must have moved the cursor somewhere in that.

Just to repeat myself. This could be done better. If you had a Style for your ToC title (say, called myToCStyle) you could replace:Selection.TypeText Text:="TABLE OF CONTENTS"
Selection.MoveLeft Unit:=wdCharacter, Count:=17, Extend:=wdExtend
Selection.Font.Name = "Garamond"
Selection.Font.Size = 14
Selection.Font.Bold = True
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.TypeParagraph with:With Selection
.Style = "myToCStyle"
.TypeText Text:="table of contents" & vbCrLf
End WithYou could have the Style set for uppercase, centered, AND whatever space you want following - instead of those "extra" TypeParagraphs.

No need to use full caps...the style takes care of that.
No need for centering instruction...the style takes care of that.
No need for selecting your text again to format it...the style takes care of that.
No need to add extra paragraphs...the style takes care of that.
No need for a Bold instruction...the style takes care of that.
No need for a font size instruction...the style takes care of that.

Ahem.