Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Creating a TOC (XP)

    I would like to create a table of contents based on the "Subject" of each question in the attached document. If there's a way to automate this process that would be great, because the entire documents has over 100 questions/subjects. Thanks!
    Attached Files Attached Files

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a TOC (XP)

    Hi Rick:
    I'm not a VBA expert, but here's a start & maybe one of the VBA experts can make it loop.
    Step 1: Set up your heading 1 style to match what you want your subject to look like.
    Step 2: Run this macro, which will find the word "Subject", move 1 cell to the right & change the subject to the Heading 1 style.<pre>Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 1/7/04 by Phil Rabichow
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "subject"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCell
    Selection.Paragraphs.OutlinePromote
    Selection.Find.ClearFormatting
    End Sub</pre>

    Step 3: Create an outline based on the Heading 1 style, using Insert/Index & Tables/Tables of Content & base it on Heading 1 style only..

    Note: You could change this to any style that you wished to use.
    Hope this helps,

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating a TOC (XP)

    Note: Phil's solution is even better.

    It would have been easiest if you had used a distinct style for the subject. You could then have created tha TOC from this style. In the present situation it may be easier to create a TOC based on TOC entries. The following macro will do this, but be warned: if the word "subject" occurs anywhere else in the document except to indicate the subject of a question, it will cause mayhem. Do the following:
    - Switch to the Visual Basic Editor (Alt+F11)
    - Select Tools | References...
    - Locate Microsoft Forms 2.0 Object Library and tick the corresponding check box.
    - Click OK.
    - Select Insert | Module.
    - Paste the code into the module.
    - Position the insertion point somewhere in the macro.
    - Select Run | Run Sub/UserForm or press F5.
    - Switch to Word (Alt+F11) to view the result.

    Sub CreateTOC()
    Dim MyData As New MSForms.DataObject
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Text = "Subject"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do While .Execute
    Selection.MoveRight Unit:=wdCell
    Selection.Copy
    MyData.GetFromClipboard
    Selection.EndKey Unit:=wdLine
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldTOCEntry, _
    Text:=Chr(34) & MyData.GetText & Chr(34)
    Loop
    End With
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdPageBreak
    With ActiveDocument
    .TablesOfContents.Add Range:=Selection.Range, _
    UseHeadingStyles:=False, UseFields:=True
    End With
    MyData.Clear
    Set MyData = Nothing
    End Sub

  4. #4
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Creating a TOC (XP)

    Hi Hans,

    <hr>Note: Phil's solution is even better.<hr>

    I'll stick with Phil's macro. Thanks for your help though!

  5. #5
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Creating a TOC (XP)

    <P ID="edit" class=small>(Edited by Jon_Rick on 07-Jan-04 11:34. )</P>Hi Phil,

    That macro works great; thanks! One follow-up:

    When I scroll the document using the right-side pane, a display named "Page: 1," "Page: 2," appears. Is there a way I can add that page's Heading 1 to this display?

    Thanks again.

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a TOC (XP)

    Hi Jon:
    I don't know of a way to add the heading to the display. I do know that scrolling is controlled by entries in the registry, but I don't know if there is a key that controls what is displayed.
    Cheers,

Posting Permissions

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