Share via

Copying headers and footers between docs programmatically

Anonymous
2013-07-31T15:43:03+00:00

Hi all,

I have inherited a macro that copies the headers and footers between documents (this is done as our company is multi-site and sometimes a file gets assigned to another office and they want to take an existing doc and "swap" in programmatically the headers/footers for the new office).

Here are the details:

  • different first page header/footer from the rest of the doc

so the macro fires and here is framework of the code:

  • open up a blank doc using the template for the new location (e.g. "Halifax_LtrHd.dot") as a new doc; this will be the "source" for the header/footer info
  • switch back and forth between the "original" doc and the new doc copying the headers/footers

The previous guy just recorded the macro.  I am slimming it down and trying to make it more bullet proof.

The question: I found that the "source" document needs to have a section break added to the bottom of it in order to "reveal" the header and footer for the 2nd page onward.  Same goes for the "target" document - if it does not have more than one page, I have to go to the bottom and add a section break to force a second page.  So is there any other way to get to this "data" without "brute force" copying and pasting.

The code now looks something like this:

' change to the new document to pick up the header info from the new doc

Application.Documents(lcNewDoc).Activate

' open up the header

ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader

' select the whole "story" i.e. everything in the header and copy it

Selection.WholeStory

Selection.Copy

' activate the original document and open it's header

Application.Documents(lcOriginalDoc).Activate

ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader

' select that entire header (to paste over); this seems to add one paragraph marker so delete that

With Selection

   .WholeStory

   .PasteAndFormat (wdPasteDefault)

   .Delete (wdCharacter)

End With

This is what I would call brute force programming - basically doing what the person would do manually.  It uses the Selection object to move stuff around.  Is there a way to get at the header/footer data without doing this - perhaps through some other objects?

Thanks,

Albert Gostick

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Charles Kenyon 166.7K Reputation points Volunteer Moderator
    2013-08-01T14:49:14+00:00

    One other alternative that worked in a multi-office firm was to have AutoText entries for various parts that were stored on the server in each office. Those parts were set with local information. They were inserted into documents (including templates) using an AutoText field. Anytime changes were made the local AutoText holder would be updated.

    The AutoText holder (Building Blocks holder) would be a global template so its parts were available to all documents.  There was a tool kit document that had each of the fields that could be copied into templates.

    0 comments No comments
  2. Charles Kenyon 166.7K Reputation points Volunteer Moderator
    2013-07-31T21:09:53+00:00

    Headers and footers are very complex, even outside vba. Each section can have up to three different headers and footers, some of which may not even be displayed! Sections / Headers and Footers in Microsoft Word 2007-2013

    Instead, create a template with the proper headers/footers built in and copy the text from the existing document into the new document with the correct headers/footers.

    As an alternative, have the headers and footers reference custom document properties (DOCPROPERTY Fields) and change those programatically. Take a look at the Mayor & Maxey "Content Control, Variable, Bookmark and Document Property Tools" Add-In. for ideas on programming document properties.

    0 comments No comments