Unlink Headers and Autofit Footer Tables in a Word Macro
Question details
Update a Word macro to unlink headers and footers and autofit tables after creating a landscape section.
- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A macro creates a landscape section followed by a portrait section in a document that may already contain several section breaks.
- Observed behavior
- Headers and footers are not consistently unlinked from the previous section, and borderless tables do not consistently autofit to the available width.
Open the document in Word and check that its section breaks and existing macro create both the landscape section and the following portrait section. Make sure the macro can place the selection inside the landscape section before running the new code.
Process the Current and Following Sections Dynamically
Use this approach when section numbers vary between documents and the existing macro already creates the required sections.
The procedure identifies the section containing the selection and the next section in ActiveDocument. It unlinks each existing header and footer and autofits the first table in each one, if present.
Keep the existing code that creates the section breaks and sets page orientation. Replace any old header and footer processing that assumes fixed section numbers.
In Word, open Developer > Visual Basic and select the module containing your existing macro.
Add this procedure to the module, outside your existing macro: Sub UnlinkAndAutofitSections() Dim doc As Document Dim sec As Section Dim hf As HeaderFooter Dim currentIndex As Long Dim i As Long Set doc = ActiveDocument currentIndex = Selection.Information(wdActiveEndSectionNumber) If currentIndex >= doc.Sections.Count Then MsgBox "There is no following section to process." Exit Sub End If For i = currentIndex To currentIndex + 1 Set sec = doc.Sections(i) For Each hf In sec.Headers If hf.Exists Then If i > 1 Then hf.LinkToPrevious = False If hf.Range.Tables.Count > 0 Then hf.Range.Tables(1).AutoFitBehavior wdAutoFitWindow End If End If Next hf For Each hf In sec.Footers If hf.Exists Then If i > 1 Then hf.LinkToPrevious = False If hf.Range.Tables.Count > 0 Then hf.Range.Tables(1).AutoFitBehavior wdAutoFitWindow End If End If Next hf Next i End Sub
In the existing macro, after both section breaks are created and the selection is positioned inside the landscape section, add the line: UnlinkAndAutofitSections. Remove the old code that processes hard-coded section numbers.
Run the existing macro from Developer > Macros. Click in the headers and footers of both affected sections and check that Link to Previous is off and that the first table in each existing header or footer fits the available width.

Edit Word Documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for editing Word documents. Its format compatibility makes it easy to move existing documents into WPS Writer, although it does not fix Microsoft Word's VBA macro behavior.

Frequently Asked Questions
Where should I add the new code in my existing macro?
Add the procedure outside the existing macro in the same VBA module. Inside the existing macro, call UnlinkAndAutofitSections after creating the sections and positioning the selection in the landscape section.
Why shouldn't I use section numbers 2 and 3?
A document may already contain section breaks, so the new sections may have different numbers. Selection.Information(wdActiveEndSectionNumber) identifies the section containing the selection at run time.
Does the code process every table in a header or footer?
No. It applies AutoFitBehavior(wdAutoFitWindow) only to the first table in each existing header or footer. Headers and footers without tables are skipped for autofitting.
What happens if there is no section after the landscape section?
The procedure displays a message and exits. Check the existing macro's section-break creation before running it again.
How can I check whether a header is still linked?
Open the header or footer in the affected section and check Link to Previous on the Header & Footer tab. The button should be off for sections that were unlinked.




