Solved

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

Posted on 2008-06-09
12
1,320 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:cool12399
  • 6
  • 6
12 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 total points
ID: 21746994
You can make all the headers and footers match the primary one with this snippet.
Sub OneHeaderOneFooter()
    Dim Sec As Section
    For Each Sec In ActiveDocument.Sections
        Sec.PageSetup.OddAndEvenPagesHeaderFooter = False
        Sec.PageSetup.DifferentFirstPageHeaderFooter = False
        Sec.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
        Sec.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
    Next Sec
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747046
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

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747083
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

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 10

Author Comment

by:cool12399
ID: 21747312
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!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747426
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
0
 
LVL 10

Author Comment

by:cool12399
ID: 21747526
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...
0
 
LVL 10

Author Comment

by:cool12399
ID: 21747535
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?
0
 
LVL 10

Author Comment

by:cool12399
ID: 21747546
lol, i think i just figured this out as well, I use the 'insertafter'?

thanks!
0
 
LVL 10

Author Comment

by:cool12399
ID: 21747636
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!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747644
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.

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747678
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.
0
 
LVL 10

Author Comment

by:cool12399
ID: 21747724
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!

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

820 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