Link to home
Start Free TrialLog in
Avatar of cool12399
cool12399

asked on

change/append all ms word header/footer text via vba

Hi,

I'm looking for how to do the following via VBA (in VB).

I want to:
(a) Get the existing header and footer, more specifically the very first header/footer set in a document (preserving any formatting, i.e., bolding/etc)
(b) Change ALL headers to be only ONE header (i.e., the first header I extracted), this is true for footers as well (so if there were 5 different headers in a document, they would all be set to just one)
(c) And also to be able to 'append' a header to all existing headers (doesn't matter if different header text, it would be appended to the existing header present on a certain page).

I know I have to do something with

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range = "something",
but not exactly sure what to do.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will copy a particular header to all the rest. The First page header for section 1 is used for the example
Sub CopyOneHeader()
    Dim Sec As Section
    Dim hdr As HeaderFooter
    
    ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Copy
    For Each Sec In ActiveDocument.Sections
        For Each hdr In Sec.Headers
            hdr.Range.Paste
        Next hdr
    Next Sec
End Sub

Open in new window

This will append the header text to the existing headers.
Sub AppendOneHeader()
    Dim Sec As Section
    Dim hdr As HeaderFooter
    Dim rng As Range
    
    ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Copy
    For Each Sec In ActiveDocument.Sections
        For Each hdr In Sec.Headers
            Set rng = hdr.Range
            rng.Collapse wdCollapseEnd
            rng.Paste
        Next hdr
    Next Sec
End Sub

Open in new window

Avatar of cool12399
cool12399

ASKER

Ok, just working through this -- how would I set it to a specific value though as well, i.e., say I wanted the header to be 'myheader', not necessarily from another document, how do I set the headers to say "My Header"?

I.e.,

        For Each hdr In Sec.Headers
            hdr="My Header"
        Next hdr

?

Thanks!
You have to set the text for the header's range. This will do it for all three headers in a section.

        For Each hdr In Sec.Headers
            hdr.Range.Text = "My Header"
        Next hdr
haha, thanks, just figured that out :)

Now I'm trying to figure out how to 'add'/'append' with a document's header...
so if a document has text with formatting, using that header and adding something like "myinfo"

i.e.,

hdr.Range.Text = hdr.Range.ExistingHeader (to use existing formatting), then plus other text...
p.s., I appreciate your help, is there some website you learned all of this from (for me it seems like I have to 'guess' with the object browser), or did you just learn it on your own?
lol, i think i just figured this out as well, I use the 'insertafter'?

thanks!
Ok,

I'm getting some wierd errors...

It 'seemed' to be working ok, but now all of the sudden when I run it on the same document, it doesn't add anything.

To get a header, I am using:

Set headerDoc = wdApp.Documents.Open("c:\test.doc", passworddocument:='password", writepassworddocument:="password")

And for my file:
Set wdDoc1 = wdApp.Documents.Open("c:\mydoc.doc", passworddocument:='password", writepassworddocument:="password")

Then when I want to 'set' the header, I am using:
headerDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Copy
For Each Sec In wdDoc1.Sections
  For Each hdr In Sec.Headers
    hdr.Range.Paste
  Next hdr
Next Sec

And then I save it. However, it doesn't seem to be 'saving' it to the correct value (actually doesn't seem to be making *any* changes at all). Any ideas?

Thanks!
Dim rng as Range
Set rng = hdr.Range.Text
rng.Collapse wdCollapseEnd
rng.Text = "My New text"

I started using VB version 1 and Word in DOS, but I've learned a lot by trying to help out here.

Sorry that last comment was to your previous two.

The only thing I can see, and it is probably not in your actual code is a bit of confusion with the single/double quotes around the passwords.
Hmm, no, it wasn't the password (i typed that inmyself).

Ahhhhhhhhhhhhhhhh lol. I figured it out...

For some reason (dunno why), but when I opened my modified document it was opening it in 'normal' view, so didn't show the new headers/footers... When I changed it to 'print' view, I was then able to see the correct header/footer...

Thanks for your help!