I'd like to make a macro that replaces the current header and foot

J

Jean-Guy Marcil

rob was telling us:
rob nous racontait que :
That would be great Jean,

How do I do that?

Well, I don't have time right now to code the whole thing for you...

But, in general..

Set a range to the header you want, like:

Dim HeaderRange As Range

Set HeaderRange =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range

Then manipulate the range to get the insertion where you want it, as in:

HeaderRange.Collapse wdCollapseEnd

or you may have done that to some extent before, when setting the range:

Set HeaderRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
_
.Range.Paragraphs(1).Range
HeaderRange.Collapse wdCollapseEnd

And finally, insert the autotext:

With HeaderRange
.Text = "MyAutoTextName"
.InsertAutoText
End With

See the help file for the InsertAutoText method for more information.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

rob

Thanks Jean,

though the only experience i have with VB programming is the last day or two
with you and jezebel so is it possible you could write out the code that
would make this happen? I've been trying to make since of it for the past
day but I'm not to quick at this.

Many Thanks,

RP
 
J

Jean-Guy Marcil

rob was telling us:
rob nous racontait que :
Thanks Jean,

though the only experience i have with VB programming is the last day
or two with you and jezebel so is it possible you could write out the
code that would make this happen? I've been trying to make since of
it for the past day but I'm not to quick at this.

Like I wrote before, it would look like this:

Dim HeaderRange As Range

'Change the numbers in the ( ) to reflect the sections
'or the paragaph you want
Set HeaderRange = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary) _
.Range.Paragraphs(1).Range
'If you want the header before the selcted paragraph, change
'wdCollapseEnd to wdCollapseStart
HeaderRange.Collapse wdCollapseEnd

'Replace "MyAutoTextName" by the actual name of the AutoText
With HeaderRange
.Text = "MyAutoTextName"
.InsertAutoText
End With

Now, you'd probably need a userform to ask the user to select a header from
a list using a Dropdown list and an OK button.
For more on this, see
http://word.mvps.org/faqs/Userforms/index.htm

If you need help with that aspect, start a new thread in the userform group.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

rob

Jean,

Your the greatest!!! Thank you for explaining in detail what all of this
means. Lol..though of course there is one more problem. This doesn't
replace the original header. How do I tell word to erase the current header
and then paste in the autotext?

Many Many Thanks,

RP
 
R

rob

Forgot to ask you....how do I make this work with the footer now? I tried
replicating the code and replacing the word headers with footers but i had
this error on line 16: Invalid or Unqualified Reference and Range is
highlighted in Blue

Sub Test1()
1 Dim HeaderRange As Range
2
3Set HeaderRange = ActiveDocument.Sections(1) _
4 .Headers(wdHeaderFooterPrimary) _
5 .Range.Paragraphs(1).Range
6
7With HeaderRange
8 .Text = "CA Header for PDF"
9 .InsertAutoText
10End With
11
12Dim FooterRange As Range
13
14Set FooterRange = ActiveDocument.Sections(1) _
15 .Footers(wdHeaderFooterPrimary)
16 .Range.Paragraphs(1).Range
17
18With FooterRange
19 .Text = "CA Footer for PDF"
20 .InsertAutoText
21End With
22
End Sub
 
J

Jean-Guy Marcil

rob was telling us:
rob nous racontait que :
Forgot to ask you....how do I make this work with the footer now? I
tried replicating the code and replacing the word headers with
footers but i had this error on line 16: Invalid or Unqualified
Reference and Range is highlighted in Blue
YOu are missing a " _" at the end of line 15:

.Footers(wdHeaderFooterPrimary)
should be
.Footers(wdHeaderFooterPrimary) _

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

rob was telling us:
rob nous racontait que :
Jean,

Your the greatest!!! Thank you for explaining in detail what all of
this means. Lol..though of course there is one more problem. This
doesn't replace the original header. How do I tell word to erase the
current header and then paste in the autotext?

As you wrote back, you can remove the collapse bit, but if the header is
made of many paragraphs, then this won't be enough.

Then, try this:

Set HeaderRange = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range

which will set a range to the whole header content.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top