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

Word Macro for placing Headers into Section 2 only

Status
Not open for further replies.

DaveyCricket

Technical User
Jan 28, 2009
2
US
I'm a complete VB noobie.

I'm trying to create a macro for Word that will create headers from Page 3 onward, but leave Pages 1 and 2 without. I've created the one below by simply recording the macro and taking all the steps I would normally take to make this possible, (Insert>Break>Next Page>View>Header Footer>Show Next>Same as Previous, etc.)

I also should note that I've added a couple Inputboxes to type in some text, though this error occurs with or without those prompts.

Here's the code:

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
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Company = InputBox("Insert Company Name ALL CAPS")
Selection.TypeText Text:=Company
Selection.TypeParagraph
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
Selection.TypeParagraph
Quote = InputBox("Type Quote Number")
Selection.TypeText Text:="Quote No. "
Selection.TypeText Text:=Quote
Selection.TypeParagraph
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub



The error occurs on both these lines:

ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious

When I ' out those two lines, the macro works beautifully, but populates the Headers for Page 1 and Page 2, but none of the ones afterward. So, it's doing exactly the OPPOSITE of what I want it to.

If anyone can tell me what I'm doing wrong, or even how to accomplish what I'm trying to, it'd be greatly appreciated.

And please, talk to me like I'm a 2 year old. Because I have pretty much no idea what I'm doing.

Thanks!
 
give this a try

Code:
Selection.InsertBreak Type:=wdSectionBreakNextPage
  Company = InputBox("Insert Company Name ALL CAPS")
  Quote = InputBox("Type Quote Number")
    
    CurSection = Selection.Information(wdActiveEndSectionNumber)

    ActiveDocument.Sections(CurSection).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    ActiveDocument.Sections(CurSection).Headers(wdHeaderFooterPrimary).Range.Select
     
     
    
    Selection.TypeText Text:=Company
    Selection.TypeParagraph
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
    Selection.TypeParagraph
    
    Selection.TypeText Text:="Quote No. "
    Selection.TypeText Text:=Quote
    Selection.TypeParagraph
    Selection.TypeText Text:="Page "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
   
    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

ck1999
 
The recorder (of necessity) records actions in the UI that are not always necessary, or even desirable, when working from code. The effect is extreme in the case of working with Headers and Footers.

Try this:
Code:
[blue]    Company = InputBox("Insert Company Name ALL CAPS")
    Quote = InputBox("Type Quote Number")
    
    ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3) _
        .InsertBreak wdSectionBreakNextPage
    
    With ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary)
        .LinkToPrevious = False
        With .Range
            .InsertAfter Text:=Company
            .InsertParagraphAfter
            .Collapse wdCollapseEnd
            .Fields.Add Range:=.Duplicate, Type:=wdFieldDate
        End With
        With .Range
            .InsertParagraphAfter
            .InsertAfter Text:="Quote No. " & Quote
            .InsertParagraphAfter
            .InsertAfter Text:="Page "
            .Collapse wdCollapseEnd
            .Fields.Add Range:=.Duplicate, Type:=wdFieldPage
        End With
    End With[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top