I'm using a macro based on one found in this forum to split a document into its constituent 'chapters'. Each chapter starts with "Heading 1" and finishes with a section break because each chapter has a unique primary header and primary footer.
The split out chapter therefore contains an extraneous page at the end which comes after the section break.
To remove the extraneous page and preserve headers and footers I need to link the last section of the split out document to the previous. However it appears that it is not sufficient to link just the primary headers and footers, it is also necessary to link the first page, primary and even page headers and footers (six items altogether). Deleting the section break without linking all 6 items results in the headers and footers disappearing.
This is the code I am using to do the linking
Code:
Sub sbRemoveLastPage(ThisDoc As Document)
Dim myrange As Range
On Error GoTo foundError
With ThisDoc.StoryRanges(wdMainTextStory).Sections.Last
.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = True
.Headers(wdHeaderFooterEvenPages).LinkToPrevious = True
.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = True
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = True
End With
Set myrange = ThisDoc.StoryRanges(wdMainTextStory).Characters.Last
Do While myrange.Characters.Last.Previous = ChrW(12) Or myrange.Characters.Last.Previous = ChrW(13)
myrange.Characters.Last.Previous.Delete
Loop
Exit Sub
foundError:
MsgBox "We got an error", vbOKOnly
If Err.Number = 4605 Then
' retry linking
ThisDoc.StoryRanges(wdMainTextStory).Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
Resume Next
End If
End Sub
This code may process one or more chapters before stopping with error 4605 (No previous section - which should not be the case). If I elect to debug and press F5 the macro happily continues but may stop again with the same error 4605. Single stepping with F8 also works fine.
This problem has been described elsewhere and marked as resolved.
https://social.msdn.microsoft.com/Fo...?forum=worddev
I've tried all of the solutions proposed in the link above but I get the same problem with all of them.
If I add a pause at the start of the code above it progresses further but then generates a different error
error 5496 - check that the drive door is closed.
Last week this code worked without a hitch.
So the issues are
1. What is the easiest way to merge two section preserving the header and footer from the first section
2. Why is the on error statement not operating. I never see the msgbox just error dialog boxes.