Solved

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

Posted on 2008-06-09
12
1,323 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: 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.

Question has a verified solution.

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

Suggested Solutions

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

734 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