[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2131
  • Last Modified:

Record a macro in word to insert header and footer


 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,
3 Solutions
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
lathashankarAuthor Commented:
Thank you, I wanted image in first page header and date and page numbers in subsequent page, any code to this??

thank you
Anne TroyEast Coast ManagerCommented:
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.
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
Anne TroyEast Coast ManagerCommented:

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now