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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro with multiple Headers

Status
Not open for further replies.

ebrooks54

IS-IT--Management
Dec 4, 2002
54
US
I have a document that has 2 Headers in it that Word Identifies as "First Page Header", and "Header". The FPH is the same as the Header, except that it contains a graphic. The document is 7 pages long. Immediately after loading the document, I activate the macro recorder and do the following:
[red]
Ctrl-End
View Header/Footer
Click "Same as Previous" button
(Deactivating SaP for the header)
Ctrl-A
Delete
Author Index
Ctrl-A
Click Bold / Center / Bottom Border
Close Header/Footer ToolBar
[/red]
...and then stop recording.

The document looks perfect. An 8th page has been added with a header that says "Author Index". The first 7 pages are all the way they should be.

I then close the document without saving it, then re-open it. I then run the macro, and the results are just plain weird.

The Author Index header is at the top of Page 1. The header with the graphic is on page 8. Obviously, that is exactly the opposite of what should happen.

The recorded code follows. Answers/suggestions would be greatly appreciated.

Code:
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdSectionBreakNextPage
    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.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
        LinkToPrevious
    Selection.WholeStory
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Author Index"
    Selection.WholeStory
    Selection.Font.Bold = wdToggle
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    With Selection.Borders(wdBorderBottom)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
 
The clue is the following line in your code:

[COLOR=red yellow] Selection.InsertBreak Type:=wdSectionBreakNextPage[/color red yellow]

Here is what happened, and I know it is going to sounds weird. For clarity I am stating that FirstPageHeader(and its text) = FP(first), the other page headers (and its text) = Other(other). I hope that is clear.

go to the end of the document.
insert a Section break.
STATUS: previous section
FP(first), Other(Other)
NEW section
FP(first)

go to headerfooter mode
kill LinkToPrevious
SELECT WholeStory and delete
STATUS: previous section
FP(blank, Other(other)
NEW section
FP(first)
[COLOR=red yellow]deletes the wholestory of wdHeaderFooterFirstPage[/color red yellow]

type "Author Index"
and the rest of the stuff
STATUS: previous section
FP(Author Index), Other(other)
NEW section
FP(first)
[COLOR=red yellow]writes the wholestory of wdHeaderFooterFirstPage. This sets the story, BUT, because you set the NEW section to not Same as previous, the first page of the previous section is changed, but the new section is not.[/color red yellow]

There are three possible things that made it go wrong.

1. You used a Section break - do you actually need a section break?

2. You used WholeStory to delete. What exactly are you trying to change with the delete?

3. You did not change the Page Setup for the new section - do you still want a Different first page?

Please describe exactly what you want to occur. This can be fixed.

Gerry
 
Thanks for the response. What I am trying to do:

This is part of the automated generation of an order form. There is a mailmerge that brings rows of Excel data into a Word Table.

On page 1 of the resulting document there is a header (FP)that contains a graphic of an order header for customer and shipping data, as well as column headings for the excel data being merged. On subsequent pages, the header only contains the column headings of the Excel data.

After the mailmerge is complete, the next step is to automate the generation of an "Author Index". The first part of the macro needs to go to the end of the document, start on a new page, set the header for that and subsequent pages, and exit. The second part of the macro, which already works, is to generate the index.

I used a section break because I was given to understand that that was the proper way to keep headers and indexes separate.
 
On further inspection, and single-stepping through the macro, I have found where the problem is occurring.

When recording the macro, clicking on the "Same as Previous" button did not shift focus away from the FPH-Sect 2. Pressing Ctrl-A then selected the contents of the correct header.

For some reason, when playing back the macro, the statement:
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious

...shifted focus to the FPH-Sect 1. From that point, the following statements are acting on the wrong header.

I guess the immediate question is how to keep the focus on the right header? The ultimate question is why does playing macro provide different results than when it was recorded?

What statement do I need to make sure that right header is selected?
 
There is a certain satisfaction to getting your own answers, and I don't know if there is a better way, but here is the working code for this problem:

Code:
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdSectionBreakNextPage
    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.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
        LinkToPrevious
[red]          
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.EndKey Unit:=wdStory
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
[/red]
    Selection.WholeStory
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Author Index"
    Selection.WholeStory
    Selection.Font.Bold = wdToggle
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    With Selection.Borders(wdBorderBottom)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

I don't now why when playing the macro focus shifted to the FPH on page 1 of the document, but the code noted above in red forces the macro back to the one I wanted to act on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top