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

x
  • 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?

Thanks
Sean
0
SeanNij
Asked:
SeanNij
  • 4
1 Solution
 
rockiroadsCommented:
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

0
 
rockiroadsCommented:
I guess u can ignore 2 now that its not applicable to rtf
0
 
rockiroadsCommented:
I wonder once u created the document, is to use Word Automation to open the generated RTF then add in your headers/footers?

e.g.

'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

or

        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
e.g.

set tracking on
m_objWordDoc.TrackRevisions = True

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


So whatever the code generated is, u use m_objWordDoc

0
 
rockiroadsCommented:
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
        objWordDoc.ActiveWindow.Panes(2).Close
    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.Selection.TypeParagraph
    objWordDoc.ActiveWindow.ActivePane.LargeScroll Down:=1
    If objWordDoc.ActiveWindow.Selection.HeaderFooter.IsHeader = True Then
        objWordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Else
        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"
   
    objWordDoc.Close
    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

0

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