I am trying to apply a new company brand design to existing document templates for future use. It works fairly well but many document templates currently only have one page so the full page background image with header and footer is the same on all pages, which might work fine but I would like to prepare for cases where they become suddenly longer than one page. Then I don't need the header to include an adress field for windowed envelopes, so I need to convert the document to have a different first page.
If activating this via Page Setup, all content of the header (which is only text boxes apart from the full page background image) is moved to the non-existing second page and the new first page header is empty.
My idea to work around that was to select all text boxes, cut or copy them to the clipboard, convert the header, then paste the content from the clipboard back on the new first page header.
That way, the second+ pages are clean and the first page stays the same. I already had that running until I closed all instances of word to freshly test the macro and now I am getting "Runtime Error 4198 Command failed" at setting the first page different but I think it is more likely to be caused by the Selection.Cut before or Word just crashes completely.
Sub ConvertToFirstPageDifferent()
'
' Converts document to First Page Different and prevents Headers on the first Page from being lost.
'
'
Dim Sh As Shape
For Each Sh In ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Shapes
If Sh.Type = 17 Then Sh.Select False
Next
Selection.Cut
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterFirstPage).Range.Paste
End Sub
Debugger complains about ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Now as browsing on the net for examples I often read that Range is to be prefered to Selection but I'm unable to find how I can use Range to select only certain objects.
What is the problem? How could I accomplish keeping those text boxes differently?
All you need is something like:
Sub Demo()
With ActiveDocument.Sections.First
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterFirstPage).Range.FormattedText = .Headers(wdHeaderFooterPrimary).Range.FormattedText
.Headers(wdHeaderFooterFirstPage).Range.Characters.Last.Previous.Delete
.Headers(wdHeaderFooterPrimary).Range.Text = vbNullString
End With
End Sub