Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: auto-TOC in word when normal.dot is launched

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location

    Question auto-TOC in word when normal.dot is launched

    Hi there.

    Could anyone help me with the following:

    I'd like to build a macro that does the following this to a document:

    1. Insert new page at the beginning of the dokument.
    2. Remove header for the first page
    3. Find and replace all entries that have the format times new roman, bold, font-size 13 with the style "header 3"
    4. Find and replace all entries that have the format times new roman, bold, font-size 11 with the style header 3 (not changing the font size)
    5. Insert tabel of contents one the first page
    6. Format the TOC with Formal
    7. Format the TOC with double lines
    8. Fomat the TOC Bold
    9. Insert page numbers beginning from page 2 or wichever page that comes after the TOC

    The TOC should be created everytime a certain template is loaded.

    Quite a mouthfull, but can it be done?

  2. #2
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    Why not just build your template like that to begin with?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    Exactly! But how?

    Isn't it something you include as a vba?

  4. #4
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    Do you have a document that looks like what you describe above...if so post it here....hit "post reply" in the lower left corner of the last post and post your message...then scroll down to find the button that says "manage attachements"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location

    no document as mentioned

    No I don't have taht kind of document.

    The thing is a have an application taht produces a list of an archive-inventory, but I need to do all the formatting when the normal.dot is called to produce the list.

  6. #6
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    I guess I missed that item number in the list.

    What format is produced by the application? Is it Word? This starts to sound like something besides a template.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    I guess it's rtf

  8. #8
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    Could you post a sanitized sample? Remove any proprietary info first.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    This is not a small task so you will have to do a lot of the work yourself. We can help you get started and then you can post specific questions. I would take it one step at a time.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    i'll post it later. The kids are awake.'

    Look forward to completing the task

  11. #11
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    Try this for importing the rtf into a new document....might give you some ideas.
    [VBA]Option Explicit
    Sub test()
    Dim doc As Document
    Dim r As Range
    Dim num_files As Integer
    Dim file_name As String
    Dim dir_path As String
    Dim file_ext As String
    Dim lngPage As Long

    Application.ScreenUpdating = False
    Set doc = Application.Documents.Add
    ' Set doc = ThisDocument

    dir_path = "F:\Temp\"
    file_ext = "rtf"

    file_name = Dir(dir_path & "*." & file_ext)
    Do While Len(file_name) > 0
    Documents.Open FileName:=dir_path & file_name
    lngPage = ActiveDocument.PageSetup.Orientation
    Set r = Selection.Range
    r.WholeStory
    r.Copy
    ActiveDocument.Close wdDoNotSaveChanges
    With Selection
    With .Sections(1).PageSetup
    .Orientation = lngPage
    .SectionStart = wdSectionNewPage
    End With
    .PasteAndFormat wdFormatOriginalFormatting
    file_name = Dir()
    If Len(file_name) > 0 Then
    .InsertBreak Type:=wdSectionBreakNextPage
    End If
    .EndKey unit:=wdStory
    End With
    Loop

    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    Adding this in the end of the code would do the things I want to. At least some of it?

    Sub Macro1()
    '
    '
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend
    Selection.Find.ClearFormatting
    With Selection.Find.Font
    .Size = 13
    .Bold = True
    End With
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 3 Char")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.HomeKey Unit:=wdStory
    Selection.InsertBreak Type:=wdPageBreak
    Selection.HomeKey Unit:=wdStory
    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.WholeStory
    Selection.Delete
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "TOC ", PreserveFormatting:=False
    End Sub

  13. #13
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location

    Example of rtf-file

    Hi Lucas.

    Here's an example of an rtf file.

    Nicolai

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Nicolai,

    1. This is cross-posted on Tek-Tips.

    2. I posted a code solution there.

    3. When I did so, I had not seen your sample file. ALWAYS try and give good information, and details. It is VERY, very, significant that all the text in your file is in a table. This should have been mentioned.

    I am not sure if the code solution I posted on your thread at Tek-Tips will work quite properly, as it was not set up for a table. As no table was mentioned.

    I will not adjust it for a table. You will have to try and do that.

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Also, you should not be doing this from Normal.dot. You should be doing this from another template.

  16. #16
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    Hi fumei.

    Yes i saw your post and your code, and its working fine. The only thing i might have to do is to alter the heading 3 properties to meet my needs.

    Maybe some of the code could be included in the rtf-file so the TOC would be created on the fly?

    Nicolai

  17. #17
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    and...

    i'm a totally newbie at VBA. Any good suggestions to what to read?

  18. #18
    VBAX Regular
    Joined
    Oct 2007
    Posts
    11
    Location
    Actually i didn't know this..

    3. When I did so, I had not seen your sample file. ALWAYS try and give good information, and details. It is VERY, very, significant that all the text in your file is in a table. This should have been mentioned.

  19. #19
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    Quote Originally Posted by fumei
    Nicolai,

    1. This is cross-posted on Tek-Tips.
    Click here for an explanation of cross-posting.....Always share a link


    You should take a few minutes to read the FAQ found at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Have you ever actually tried working with tables? Text in tables is always a significant point.

    So you read, and are using my code solution I posted on Tek-Tips...and you did not bother to respond?

    I am sorry I posted the solution. You are on your own. I will not be posting further.

Posting Permissions

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