Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WORD 2010 Macro for Editing Headers & Footers

Tags:

ms-word

vba

I have only basic VBA experince and my prior Macro experence was primarily with WORD 2003. Recording Macros used to take GoToFooter (or Edit Footer) Menu Commands and allow subsequent editing. In WORD 2010, this (and many other) commands do not "record" to the Macro (yet when in Record mode, I do get into Edit Footer function).

A research of various VBS options shows several ways to create Footers and to make global Footer setting changes within Macro. However If I simply want to Revise the Company name within the Footer (for example), I can find no way to do this within a Macro subroutine.

This subroutine is one that I would call from the Main Macro that is stepping through each file in a Folder (& subfolders). I have the main Macro functioning.

Does WORD 2010 Macro-VBA preclude simple Edit-Footer function?

Thanks in advance

So, thanks to Issun, here is my solution:

`
Sub Sub_FTR_0()
'
ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

For i = 1 To ActiveDocument.Sections.Count
 'REM: INSERT Code from RECORD MACRO recorded when editing one Footer correctly
    Selection. [[xxx]], etc.

If i = ActiveDocument.Sections.Count Then GoTo Line1

    ActiveDocument.ActiveWindow.ActivePane.View.NextHeaderFooter

Line1:
Next

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub
`
like image 516
Dan Avatar asked Nov 15 '11 03:11

Dan


People also ask

How do I edit a header in Word 2010?

Double-click the header or footer you want to edit, or select Header or Footer, and then select Edit Header or Edit Footer. Add or change text for the header or footer or do any of the following: To remove the first page header or footer Select Different First Page.


2 Answers

Here is a way you can access the headers/footers via VBA. As you can see, it's rather complicated syntax to get to something so simple :p there

Sub EditHeadersAndFooters()

Dim i As Long

For i = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i)
        .Headers(wdHeaderFooterPrimary).Range.Text = "Foo"
        .Footers(wdHeaderFooterPrimary).Range.Text = "Bar"
    End With
Next

End Sub

Here is a link to example code on how to change the headers in every file in a folder. It takes a different approach and I have never tried it, but for your reference: http://www.vbaexpress.com/kb/getarticle.php?kb_id=45

like image 72
aevanko Avatar answered Oct 19 '22 04:10

aevanko


This worked for me for all pages in the document.

word.ActiveDocument.Sections(1).Headers(1).Range.Text = "Put the header here"

like image 35
Gabriela M Avatar answered Oct 19 '22 05:10

Gabriela M