Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2018, 07:02 PM
bgarrett bgarrett is offline VBA  to Delete Single Page Windows 10 VBA  to Delete Single Page Office 2016
Novice
VBA  to Delete Single Page
 
Join Date: Jan 2018
Posts: 3
bgarrett is on a distinguished road
Default VBA to Delete Single Page

Hello All!



I’m trying to figure out how to delete a single page in word (including its header/footer). There’s code out there that I can use to delete the content from a page (but running that code leaves the header/footer behind), and there’s code I have found that will delete the header/footer from every single section in the entire document, but what I want to do lies in the middle of those two.

I want to be able to delete the current page (including header/footer) from the document.
Reply With Quote
  #2  
Old 01-30-2018, 07:29 PM
macropod's Avatar
macropod macropod is offline VBA  to Delete Single Page Windows 7 64bit VBA  to Delete Single Page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Is the page you want to delete represent the entire Section to be deleted? If not, deleting the header/footer will delete it for every other page using the same header/footer. Also, if the header/footer of a following Section is linked to this one, that will get deleted, too.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-30-2018, 07:49 PM
bgarrett bgarrett is offline VBA  to Delete Single Page Windows 10 VBA  to Delete Single Page Office 2016
Novice
VBA  to Delete Single Page
 
Join Date: Jan 2018
Posts: 3
bgarrett is on a distinguished road
Default

Hello!

Yes, what I’d like to do essentially is delete the current section including its header/footer. I’m not worried about anything being linked, because it shouldn’t be. Thanks!
Reply With Quote
  #4  
Old 01-30-2018, 08:10 PM
macropod's Avatar
macropod macropod is offline VBA  to Delete Single Page Windows 7 64bit VBA  to Delete Single Page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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
  #5  
Old 01-31-2018, 07:59 PM
bgarrett bgarrett is offline VBA  to Delete Single Page Windows 10 VBA  to Delete Single Page Office 2016
Novice
VBA  to Delete Single Page
 
Join Date: Jan 2018
Posts: 3
bgarrett is on a distinguished road
Default

Sorry I’m just now replying, things got pretty crazy. Just wanted to say THANK YOU!! This worked PERFECTLY!!!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA  to Delete Single Page Delete Character on Single Line Only Tye30 Word VBA 8 04-20-2017 08:40 PM
Is there a way to exctract a single page from Word? mobileone Word 1 04-05-2017 02:27 PM
convert booklet to A4 single page mickfromoz Publisher 0 10-15-2016 05:28 PM
VBA  to Delete Single Page How do you view a single page delboy Word 29 01-25-2013 06:42 PM
VBA  to Delete Single Page single page formatting Ridgerunner Word 8 01-29-2012 06:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft