Solved

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

Posted on 2008-06-09
12
1,317 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now