Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-31-2020, 02:35 PM
ajgarrison ajgarrison is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2013
Novice
VBA deleting sections retains last section header and footer
 
Join Date: Sep 2020
Posts: 14
ajgarrison is on a distinguished road
Default VBA deleting sections retains last section header and footer

I am opening a WORD document from MS Access, populating fields, deleting all unneeded sections and printing. Each section has it's own header and footer. When the last section deleted is not the last section in the document everything works just find. However, when the last section deleted is the last one in the document the last page printed has the header and footer of the last section, not it's own.




Any suggestions how to address this so the last page has it's own header and footer?


Thanks in advance for all help.
Reply With Quote
  #2  
Old 11-01-2020, 12:37 AM
macropod's Avatar
macropod macropod is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2010
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

See: https://www.msofficeforums.com/20227-post6.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-01-2020, 11:46 AM
ajgarrison ajgarrison is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2013
Novice
VBA deleting sections retains last section header and footer
 
Join Date: Sep 2020
Posts: 14
ajgarrison is on a distinguished road
Default

Thank you. I believe I found some of your code after I posted.
Code:
    'Deletes last section of a document including the section break - this fixes the problem with the last page having wrong header/footer
   
    Dim rng As Range
    Dim ctr As Integer
    Set doc = Wrd.ActiveDocument
    ctr = doc.Sections.Count
    Set rng = doc.Sections(ctr).Range
    Dim hf As HeaderFooter
    If ctr > 1 Then
        With rng
        'Added lines to "inherit" the settings from the next-to-last section
            For Each hf In .Sections(1).Headers
                hf.LinkToPrevious = True
                hf.LinkToPrevious = False
            Next
            For Each hf In .Sections(1).Footers
             hf.LinkToPrevious = True
             hf.LinkToPrevious = False
            Next

            .Select
            .MoveStart Unit:=wdCharacter, Count:=-1
            .Delete
        End With
    End If
    '--------------This addresses the blank last page-----------
    With Wrd.ActiveDocument.Characters.Last
        While .Previous.Text Like "[ " & Chr(160) & vbCr & vbTab & Chr(12) & "]"
        .Previous.Text = vbNullString
        Wend
    End With
This works perfectly except when last page is landscape, not it has changed to portrait.

Thank you.

Last edited by macropod; 11-01-2020 at 09:29 PM. Reason: Added code tags
Reply With Quote
  #4  
Old 11-01-2020, 02:46 PM
macropod's Avatar
macropod macropod is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2010
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

The code in the link I posted maintains the page layout as well.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-01-2020, 06:06 PM
ajgarrison ajgarrison is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2013
Novice
VBA deleting sections retains last section header and footer
 
Join Date: Sep 2020
Posts: 14
ajgarrison is on a distinguished road
Default

I am struggling adapting your code to my needs. My VBA is in MS Access. I am opening a Word template with 85 sections. Each section has it's own header and footer. Sections are deleted based upon criteria in Access. There are 15 different options for the sections that are to be deleted. The sections are throughout the document. Sometimes it is the very last sections and sometimes not. How do I utilize your code for the dynamic needs in MS Access?

Thank you so much for trying to help. The help is greatly appreciated.
Reply With Quote
  #6  
Old 11-01-2020, 09:28 PM
macropod's Avatar
macropod macropod is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2010
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

With the code in the link, and assuming you're using early binding, you'd replace:
Code:
Dim Sctn1 As Section, Sctn2 As Section
with code like:
Code:
Dim wdApp as New Word.Application, wdDoc as Word.Document
Dim Sctn1 As Word.Section, Sctn2 As Word.Section
You'd also replace:
Code:
With Selection
  If .Sections.Count = 1 Then
    MsgBox "Selection does not span a Section break", vbExclamation
    Exit Sub
  End If
with the document reference. For example:
Code:
Set wdDoc = wdApp.Documents.Add (template reference)
With wdDoc
and you'd replace:
Code:
Set Sctn1 = .Sections.First: Set Sctn2 = .Sections.Last
with code like:
Code:
Set Sctn1 = .Sections(x): Set Sctn2 = .Sections(y)
where 'x' is the # of the first Section to be deleted and 'y' is the # of the first Section after the last Section to be deleted.

You'd also need to replace the loop:
Code:
  While .Sections.Count > 1
    .Sections.First.Range.Characters.Last.Delete
  Wend
with code that deletes the specified unwanted Sections. For example:
Code:
  .Range(.Sections(x).Range.Start, .Sections(y).Range.Start - 1).Delete
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-01-2020, 09:37 PM
Guessed's Avatar
Guessed Guessed is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you want advice on how to fix specific issues, then you need to provide the code that is failing. In general, deleting a section break is always problematic because you need to understand where the section formatting is coming from (always from the following section break or last paragraph mark in the document.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 11-05-2020, 05:56 PM
ajgarrison ajgarrison is offline VBA deleting sections retains last section header and footer Windows 10 VBA deleting sections retains last section header and footer Office 2013
Novice
VBA deleting sections retains last section header and footer
 
Join Date: Sep 2020
Posts: 14
ajgarrison is on a distinguished road
Default

All works great! Thanks.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA deleting sections retains last section header and footer Fixed last page header/footer without the use of section breaks HelpNeed Word VBA 2 06-27-2017 06:15 PM
different header/footer sections/ page numbers Angela H Word 4 07-01-2015 12:45 PM
VBA deleting sections retains last section header and footer Mail Merge is Deleting objects in my header and footer during the merge bgranzow Mail Merge 9 06-05-2015 05:03 AM
VBA deleting sections retains last section header and footer Advanced page numbering: section pages in header, document pages in footer Albus Word 12 12-12-2014 01:36 PM
Different Header Same Footer across two sections - Letterhead bostockm Word 1 07-21-2014 05:36 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:27 AM.


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