Solved

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

Posted on 2008-06-09
12
1,318 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert .PDF 6 59
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 45
find a specific style and pass it to a access table 4 46
Resetting Word when a document is closed 5 38
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 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

12 Experts available now in Live!

Get 1:1 Help Now