Modifying MS Word Header/Footer

Posted on 2006-05-30
Last Modified: 2008-03-04
Hi all,

I'm playing out a report, from MS Access, to MS Word, using Docmd.OutputTo as RTF.

Is it possible, and if so how can i then modifiy that file to have headers and footers on each page, when opened in MS Word?

Question by:SeanNij
    LVL 65

    Expert Comment

    Not sure, the only way I could think of is

    1. Create headers/footers in your report and try it that way
    2. Try using a template file in your DoCmd.OutputTo

    LVL 65

    Expert Comment

    I guess u can ignore 2 now that its not applicable to rtf
    LVL 65

    Accepted Solution

    I wonder once u created the document, is to use Word Automation to open the generated RTF then add in your headers/footers?


    'You have to add the Microsoft Word Object Library as a reference  (Tools/References)
    Or u can define and create as object

            Dim m_objWordInstance As new Word.Application
            Dim m_objWordDoc As new Word.Document


            Dim m_objWordInstance As Object
            Dim m_objWordDoc As Object

            set m_objWordInstance = CreateObject("Word.Application")

    How to open a doc

            m_objWordInstance.Documents.Open sFile            'sFile is your fully pathed filename
            Set m_objWordDoc = m_objWordInstance.ActiveDocument

    saving document

            m_objWordDoc.SaveAs sSaveAsName

    What I suggest is u go into Word, start recording a macro
    then create a basic header and footer
    then take that generated VBA code and apply it here

    set tracking on
    m_objWordDoc.TrackRevisions = True

    replace word
            m_objWordDoc.Content.Find.Execute Findtext:=sFromWord, _
                                              ReplaceWith:=sToWord, _
                                              Replace:=Word.WdReplace.wdReplaceAll, _

    So whatever the code generated is, u use m_objWordDoc

    LVL 65

    Expert Comment

    here is sample code that opens up a word doc, which does not have any headers/footers
    then adds some, saves it as a different name

    Public Sub CreateHeaderFooter()

        Dim objWordInstance As Object
        Dim objWordDoc As Object
        Dim x As New Word.Document
        'Open Word
        Set objWordInstance = CreateObject("Word.Application")
        'Open file
        objWordInstance.Documents.Open "C:\EE\Word Auto\MyDoc.doc"
        Set objWordDoc = objWordInstance.ActiveDocument
        'Create Header and Footer
        If objWordDoc.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        End If
        If objWordDoc.ActiveWindow.ActivePane.View.Type = wdNormalView Or objWordDoc.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
            objWordDoc.ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        objWordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        objWordDoc.ActiveWindow.Selection.TypeText Text:="this is a header"
        objWordDoc.ActiveWindow.ActivePane.LargeScroll Down:=1
        objWordDoc.ActiveWindow.ActivePane.LargeScroll Down:=1
        If objWordDoc.ActiveWindow.Selection.HeaderFooter.IsHeader = True Then
            objWordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            objWordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        End If
        objWordDoc.ActiveWindow.Selection.TypeText Text:="this is the footer  "
        objWordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

        objWordDoc.SaveAs "C:\EE\Word Auto\MyDocCopy.doc"
        Set objWordDoc = Nothing
        Set objWordInstance = Nothing
    End Sub

    Like I said to u, create macro in word then look at code generated, prefix it with objWordDoc in MSAccess
    with Selection, I had to do objWordDoc.ActiveWindow


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now