PDA

View Full Version : auto-TOC in word when normal.dot is launched



vestlink
10-06-2007, 10:25 AM
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? : pray2:

lucas
10-06-2007, 10:35 AM
Why not just build your template like that to begin with?

vestlink
10-06-2007, 10:38 AM
Exactly! But how?

Isn't it something you include as a vba?

lucas
10-06-2007, 10:45 AM
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"

vestlink
10-06-2007, 10:49 AM
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.

lucas
10-06-2007, 10:54 AM
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.

vestlink
10-06-2007, 10:56 AM
I guess it's rtf

lucas
10-06-2007, 10:59 AM
Could you post a sanitized sample? Remove any proprietary info first.

lucas
10-06-2007, 11:01 AM
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.

vestlink
10-06-2007, 11:03 AM
i'll post it later. The kids are awake.'

Look forward to completing the task

lucas
10-06-2007, 11:11 AM
Try this for importing the rtf into a new document....might give you some ideas.
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

vestlink
10-06-2007, 01:32 PM
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

vestlink
10-07-2007, 12:16 PM
Hi Lucas.

Here's an example of an rtf file.

Nicolai

fumei
10-07-2007, 02:32 PM
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.

fumei
10-07-2007, 02:33 PM
Also, you should not be doing this from Normal.dot. You should be doing this from another template.

vestlink
10-07-2007, 02:46 PM
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

vestlink
10-07-2007, 02:52 PM
and...

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

vestlink
10-07-2007, 03:04 PM
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.

lucas
10-07-2007, 03:46 PM
Nicolai,

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



Click here (http://www.excelguru.ca/node/7) 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.

fumei
10-08-2007, 12:49 AM
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.

vestlink
10-08-2007, 01:45 AM
Hi.

Also her here (see also http://www.tek-tips.com/viewthread.cfm?qid=1414390&page=1) i'd like to thank all of you.

I've been hang up and had been slow on my responses.

In the future i'll be more carefull to understand the forum-rules.

Nicolai

lucas
10-08-2007, 07:23 AM
It's ok vestilink, just try to help us by providing all relevant info and sharing cross posts....