Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Modifying MS Word Header/Footer

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?

  • 4
1 Solution
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

I guess u can ignore 2 now that its not applicable to rtf
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

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


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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