Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced word macro??

Status
Not open for further replies.

vestlink

Technical User
Mar 11, 2005
13
NO
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 hte dokument.
2. Remove header for the first page
3. Find and replace all entries that have the format times new roman, bold, 13 with the style header 3
4. Insert tabel of contents one the first page
5. insert page numbers beginning from page 2.

Simple as that? Well not quite for me. So i appriciate if anyone could give me a hand here.

Nicolai
 
How about recording a macro with the mention actions, then lookin at the code

Chance,

F, G + HH
 
Id tried but it didn't work.

it falied.

Using word 2007.
 





Why make us GUESS, POSTUALTE & HYPOTHISIZE at what code you are using?

Don't you think that you might get a better answer if we knew exactly WHAT code you ae working with and exactly HOW it failed???

Please be CLEAR, CONCISE & COMPLETE.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Don't sugar-coat it, Skip, just say what you mean... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi vestlink,

Here's some code straight from the macro recorder that does part of what you want:
Code:
Sub Macro1()
'
'
    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
I've cleaned it up just a bit.

The code goes as far as inserting the TOC, skipping over step 3, which you should be able to record for yourself.

However, I think there's a problem with what you say you want to do. You say you want to "insert page numbers beginning from page 2". That part's OK, but what if the TOC is longer than 1 page? Also, do you want the page numbering to start at '1' or at '2'? If there's any chance that the TOC will extend for two or more pages or you want the page numbering to start at '1', then you should be inserting a Section break instead of a page break, then unlinking the second Section's header and footer from the first Section.

Cheers

[MS MVP - Word]
 
Thanks alot macropod.

My code looks like this now and works.

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


But:

Can you remove the question about whether you want to do the replacement in all of the document? Make the macro do all the replacements from the beginning of the document with no user feedback?

Nicolai
 
.. and yes the TOC can extend for more pages.

Nicolai
 
Replace this:
.Wrap = wdFindAsk
with this:
.Wrap = wdFindContinue

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks.

But what if I want to format the TOC with e.g. Classic style?

Nicolai
 
and make the TOC bold

Thank for all your help. The TOC is shaping up really nicely

Nicolai
 
and if i want the macro to run every time the document is opened? Like a specific template as arkivliste.dot?

Nicolai
 
Excuse me vestlink, but your last three questions? ALL of these can be answered your own research, your ownlooking and your owntrying.

Also, these are not VBA questions. You could easily answer these yourself if you understood Word.

faq219-2884

Gerry
My paintings and sculpture
 
Hi fumei.

You are quite right and you will therefore have to excuse me for my ignorance. I'm nothing but a simple peasant and may have difficulties destinquishing between vba and macro. I just thought they were part of the same thing.

And I will be looking into my OWN last three questions.

Do you by any means have any good suggestions to where I could find answers og at least good sources of where to looks?

Yours,

Nicolai
 





"...destinquishing between vba and macro..."

There is NO DIFFERENCE.

Gerry was merely pointing out that your questions are not VBA/macro questions, suitable to this forum, but questions related to the functionality of the MS Word application. If you do not understand basic MS Word functionality, you should not be attempting to code VBA Word solutions. It is putting the cart before the horse, as I am sure you would understand.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
What I am suggesting is for you to DO the things you are asking about: format the ToC as Classic, look up in Help about macros running on document open.

There is in fact a Document_Open event.

You can make the Style of the ToC whatever you want.

faq219-2884

Gerry
My paintings and sculpture
 
...way too much use of Selection.
Code:
Dim oHF As HeaderFooter
Dim oPara As Paragraph
[COLOR=red]' go to the beginning of doc
' make a new Section[/color red]
With Selection
    .HomeKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
End With
[COLOR=red]' make the NEXT Section headers unlinked[/color red]
For Each oHF In ActiveDocument.Sections(2).Headers
    oHF.LinkToPrevious = False
Next
[COLOR=red]' just in case, make all footers unlinked as well[/color red]
For Each oHF In ActiveDocument.Sections(2).Footers
    oHF.LinkToPrevious = False
Next
[COLOR=red]' make the first page have no header content[/color red]
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
    .Range.Text = ""
[COLOR=red]' replace all paragraphs Times Roman, 13, bold as Heading 3[/color red]
For Each oPara In ActiveDocument.Paragraphs
    If oPara.Range.Font.Size = 13 And _
         oPara.Range.Font.Bold = True And _
            oPara.Range.Font.Name = "Times New Roman" Then
        oPara.Style = "Heading 3"
    End If
Next
[COLOR=red]' make Section 2 start as Page 1[/color red]
    With ActiveDocument.Sections(2).Headers(1).PageNumbers
        .RestartNumberingAtSection = True
        .StartingNumber = 1
    End With
[COLOR=red]' insert ToC[/color red]
Selection.HomeKey Unit:=wdStory
    Selection.Fields.Add Range:=Selection.Range, _
        Type:=wdFieldEmpty, Text:= "TOC ", _
        PreserveFormatting:=False
You must change the attributes of the TOC styles yourself.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top