Thread: [Solved] VBA to Delete Single Page
View Single Post
 
Old 01-30-2018, 08:10 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following. most of the complication in the code is related to what happens when you delete the last Section in a document.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim HdFt As HeaderFooter, Sctn As Section
With Selection.Sections(1)
  If ActiveDocument.Sections.Count > 1 Then
    If .Index = ActiveDocument.Sections.Count Then
      Set Sctn = ActiveDocument.Sections(.Index - 1)
      With .PageSetup
        .DifferentFirstPageHeaderFooter = Sctn.PageSetup.DifferentFirstPageHeaderFooter
        .OddAndEvenPagesHeaderFooter = Sctn.PageSetup.OddAndEvenPagesHeaderFooter
      End With
      For Each HdFt In .Headers
        If Sctn.Headers(HdFt.Index).Exists Then
          .Range.FormattedText = Sctn.Headers(HdFt.Index).Range.FormattedText
          .Range.Characters.Last.Delete
        End If
      Next
      For Each HdFt In .Footers
        If Sctn.Footers(HdFt.Index).Exists Then
          .Range.FormattedText = Sctn.Footers(HdFt.Index).Range.FormattedText
          .Range.Characters.Last.Delete
        End If
      Next
      .Range.Delete
      .Range.Characters.First.Previous.Delete
    Else
      .Range.Delete
    End If
  Else
    .Range.Delete
  End If
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote