Solved

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

Posted on 2008-06-09
12
1,324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
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 Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

729 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