#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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 Thank you. Last edited by macropod; 11-01-2020 at 09:29 PM. Reason: Added code tags |
#4
|
||||
|
||||
The code in the link I posted maintains the page layout as well.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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. |
#6
|
||||
|
||||
With the code in the link, and assuming you're using early binding, you'd replace:
Code:
Dim Sctn1 As Section, Sctn2 As Section Code:
Dim wdApp as New Word.Application, wdDoc as Word.Document Dim Sctn1 As Word.Section, Sctn2 As Word.Section Code:
With Selection If .Sections.Count = 1 Then MsgBox "Selection does not span a Section break", vbExclamation Exit Sub End If Code:
Set wdDoc = wdApp.Documents.Add (template reference) With wdDoc Code:
Set Sctn1 = .Sections.First: Set Sctn2 = .Sections.Last Code:
Set Sctn1 = .Sections(x): Set Sctn2 = .Sections(y) You'd also need to replace the loop: Code:
While .Sections.Count > 1 .Sections.First.Range.Characters.Last.Delete Wend Code:
.Range(.Sections(x).Range.Start, .Sections(y).Range.Start - 1).Delete
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
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 |
#8
|
|||
|
|||
All works great! Thanks.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Mail Merge is Deleting objects in my header and footer during the merge | bgranzow | Mail Merge | 9 | 06-05-2015 05:03 AM |
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 |