PDA

View Full Version : [SOLVED:] Insert new section that is unlinked from previous and next headers



clhare
05-12-2017, 07:17 AM
I'm trying to add a new section to a document that is not linked to the header before or after the new section. This new section would have (1) text, (2), different header text plus an image, and (3) same footer. I've attached a sample document that has this section that I want to be able to insert anywhere else in the document.

I tried various different ways to accomplish this. The closest I've come is saving the new section including the page break after it as AutoText and then using the following macro to insert it elsewhere in the document:


Sub InsertSection()
' Unlink current section from previous section
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
' Return to main document
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
' Insert Appendix AutoText
Application.Templates("C:\MyPath\My Macros.dotm"). _
BuildingBlockEntries("NewSection").Insert Where:=Selection.Range, _
RichText:=True
End Sub

While I do get the new section added where I want it, the header in the new section ends up repeating in the section after it even though I tried to unlink it in the macro prior to adding the new section. I even unlinked the header from the section that follows in the sample document prior to saving it as AutoText. I can't figure out how to get this to work right!

Any help would be greatly appreciated!

Cheryl

gmaxey
05-12-2017, 07:33 AM
Try some variation of this:


Sub AddSectionAndKillLinkToPrevious()
Dim lngSectionIndex As Long
Dim lngType As Long
Dim oDoc As Word.Document
Dim oRng As Word.Range
Set oDoc = ActiveDocument
Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
lngSectionIndex = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
With oDoc.Sections(lngSectionIndex)
For lngType = 1 To 3
.Headers(lngType).LinkToPrevious = False
.Footers(lngType).LinkToPrevious = False
Next lngType
End With
'Note: lngType provides the constant value to delink the primary page
'(and the first/even page [if exists]) header/footer in the new section
End Sub

clhare
05-12-2017, 07:40 AM
OMG!!! That totally worked perfectly! All I had to do was add in the code to insert the AutoText! Thank you so much Greg!

Cheryl