none
Word 2010 - VBA Headers and footers

    Question

  • I need to do the following using VBA code.

    1. Create a new section at the end of a document.

    2. This new section must not be linked to the previous section AND it cannot be a different first page header or footer.  The new section header and footer should be completely blank.

    I appreciate ANY help with this!

    Thanks!

    Thursday, October 03, 2013 6:33 PM

Answers

  • Try:

    Sub AddSection()
    Dim HdFt As HeaderFooter
    With ActiveDocument
      With .Characters
        If .Last.Previous <> vbCr Then .Last.InsertAfter vbCr
        .Last.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
      End With
      With .Sections.Last
        For Each HdFt In .Headers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
          End With
        Next
        For Each HdFt In .Footers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
          End With
        Next
        With .PageSetup
          .DifferentFirstPageHeaderFooter = False
          .OddAndEvenPagesHeaderFooter = False
        End With
      End With
    End With
    End Sub

    Note that the above code inserts a paragraph break after the last paragraph, if needed, and removes both the 'DifferentFirstPage' and 'DifferentOddAndEven' settings, if present.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, October 03, 2013 11:19 PM
  • Here's one method... hope it helps

    Sub AddSection()
        Dim doc As Word.Document, rng As Word.Range
        Dim hdFt As Word.HeaderFooter
        
        Set doc = ActiveDocument
        Selection.EndKey Word.WdUnits.wdStory
        Selection.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
        Selection.EndKey Word.WdUnits.wdStory
        For Each hdFt In doc.Sections(doc.Sections.Count).Headers
            hdFt.LinkToPrevious = False
            Set rng = hdFt.Range
            rng.Delete
        Next
        For Each hdFt In doc.Sections(doc.Sections.Count).Footers
            hdFt.LinkToPrevious = False
            Set rng = hdFt.Range
            rng.Delete
        Next
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, October 03, 2013 10:50 PM

All replies

  • Here's one method... hope it helps

    Sub AddSection()
        Dim doc As Word.Document, rng As Word.Range
        Dim hdFt As Word.HeaderFooter
        
        Set doc = ActiveDocument
        Selection.EndKey Word.WdUnits.wdStory
        Selection.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
        Selection.EndKey Word.WdUnits.wdStory
        For Each hdFt In doc.Sections(doc.Sections.Count).Headers
            hdFt.LinkToPrevious = False
            Set rng = hdFt.Range
            rng.Delete
        Next
        For Each hdFt In doc.Sections(doc.Sections.Count).Footers
            hdFt.LinkToPrevious = False
            Set rng = hdFt.Range
            rng.Delete
        Next
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, October 03, 2013 10:50 PM
  • Try:

    Sub AddSection()
    Dim HdFt As HeaderFooter
    With ActiveDocument
      With .Characters
        If .Last.Previous <> vbCr Then .Last.InsertAfter vbCr
        .Last.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
      End With
      With .Sections.Last
        For Each HdFt In .Headers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
          End With
        Next
        For Each HdFt In .Footers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
          End With
        Next
        With .PageSetup
          .DifferentFirstPageHeaderFooter = False
          .OddAndEvenPagesHeaderFooter = False
        End With
      End With
    End With
    End Sub

    Note that the above code inserts a paragraph break after the last paragraph, if needed, and removes both the 'DifferentFirstPage' and 'DifferentOddAndEven' settings, if present.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, October 03, 2013 11:19 PM
  • Elegant Paul ... I like it! :-)

    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, October 04, 2013 2:26 AM