Consulting

Results 1 to 4 of 4

Thread: Cover Sheet and TOC Problems - Weird

  1. #1
    VBAX Newbie
    Joined
    Jun 2006
    Posts
    4
    Location

    Cover Sheet and TOC Problems - Weird

    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:

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

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Other than something you have in Sub InsertFile (not posted) I would suspect:[vba]Selection.MoveUp Unit:=wdScreen, Count:=1 [/vba]Why are you moving the Selection around?

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

  3. #3
    VBAX Newbie
    Joined
    Jun 2006
    Posts
    4
    Location
    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.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:[vba]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 [/vba]with:[vba]With Selection
    .Style = "myToCStyle"
    .TypeText Text:="table of contents" & vbCrLf
    End With[/vba]You 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.

Posting Permissions

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