Record a macro in word to insert header and footer

Posted on 2005-02-25
Medium Priority
Last Modified: 2012-05-05

 My word documents are created in fly through asp and once I receive them , I do repeated task of inserting header and footer. The first page header is different from other pages. How can I record this task as macro or any code to this will be helpful,

thank you,
Question by:lathashankar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Assisted Solution

infex earned 200 total points
ID: 13405676
Select "record new macro" in the menu macros.

Choose a keyboard combination eg. crtl-H

Do the different steps as you would do them manually.

Stop record.

Each time you have a new word document, if you use the combination crtl-H, the action will take place

Author Comment

ID: 13406441
Thank you, I wanted image in first page header and date and page numbers in subsequent page, any code to this??

thank you
LVL 22

Accepted Solution

Dreamboat earned 1000 total points
ID: 13409368
1. Create an autotext entry of your header
2. Create an autotext entry of your footer

***Suggested steps to create an autotext entry: Create your header exactly as you'd like it to appear. Hit Ctrl+A to select the entire header. Hit Insert-Autotext-New. Type myhdr as the name of it, and hit Enter. Creat the footer in the same manner, naming it myftr.

3. In a brand new, blank document, record a macro that:
--View header and footer
--File-Page setup, Layout tab, choose "different first page"
--Type myhdr and hit Enter (inside the header)
--Move to the footer
--Type myftr and hit Enter
--Hit CLOSE on the header/footer toolbar

Your autotext entries and the macro will, by default, become stored in your normal.dot file, which is the global template for Word.
You can learn how to record a macro at: http://www.theofficeexperts.com/officevba.htm#WordVBA

Make sure you give yourself a toolbar button from which to run your header/footer macro.
If you can't get the macro working, you'll at least meanwhile have autotext entries to help you.

Come back and tell us, as specifically as possible, any problems you have following the steps.

Also: I strongly suggest NOT using Ctrl keys when creating custom shortcuts. If you used Ctrl+H, for instance, you'd lose the shortcut key for the find/replace dialog box. Instead, try Alt keys for custom shortcuts. Such as Alt+H.
LVL 11

Assisted Solution

mdmackillop earned 800 total points
ID: 13409679
Having recorded your macro, look through it to remove unwanted verbiage, leaving only these lines which do the tasks you require.  eg, which set indents, margins etc, as these settings may not be applicable to all documents.  Note that the ActiveDocuments.PageSetup is reduced to one setting.
The AddPicture line in your recorded code will contain the path to your graphic.  This is best taken out and replaced with a variable, such as MyPic in the example below. This makes it simpler to change and more "portable".  
If you're feeling ambitious and you need flexibility, you can use one of the built-in dialog boxes in your code to browse to a picture as demonstrated.  By removing/inserting apostophes, you can choose which line of code to use.
Your final code might be something like this

Sub SetHeader()
Dim MyPic As String

'Option for single picture; Comment out with ' if not required
'MyPic = "C:\Pictures\rose2.jpg"

    With ActiveDocument.PageSetup
        .DifferentFirstPageHeaderFooter = True
    End With
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    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
    'Either for single picture
    'Selection.InlineShapes.AddPicture FileName:=MyPic
    'or option to choose picture
    Selection.TypeText Text:=vbTab
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
LVL 22

Expert Comment

ID: 13593796

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question