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

R

rob

Good Day,

I'd like to make a macro that deletes the current header and footer on a
word document (if there is one) and then pastes in the header and footer that
I already have created. Could someone show me an example of code that would
do this and then i can plug in what i want to be pasted into the header and
footer?

Many Thanks,

RP
 
R

rob

Thank you for your reply jezebel. Where do I put in my actual header in this
code?

Sub HeaderandFooter()

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paste

End Sub
 
R

rob

Also I need the header and footer to be in the optimale font if that is
possible.

Many Thanks

RP
 
R

rob

Also that line of code seems to just paste in what is on the clip board. How
do I make it paste exactly the letters and table I want it to?

Many thanks,

RP
 
J

Jezebel

Your original post said you wanted to paste in your new header, so I assumed
you already had the clipboard part of it worked out. Use something like

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
.Text = "This is my header"
.Font.Name = "Optima"
.Font.Size = 12
:
End with

But why are you doing this at all? Since your code has to go in a template,
why not create a template with the headers and footers already set up? It's
*much* easier to use Word itself to specify formatting than to embed
formatting instructions in code.
 
C

Charles Kenyon

You are more likely to get good results quickly with less work by creating a
template that already has the header/footer you want. Then create new
documents based on that temlate and paste text from other documents into it.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
R

rob

Well, there already is a template that has the header but when we need to
convert that type of document with that header to a pdf, the header becomes
scrambled. So with this macro it will paste in a header that isn't scrambled
by adobe:)

Thanks for the help Jezebel

RP
 
R

rob

Couple of other things about this macro. I need it to paste in a table that
is centered for the header and I need to paste in the small dot symbol from
word on the footer. Also is this the same code for footers(just replace the
header word with footer?)?

Many Thanks,

RP
 
J

Jezebel

Check the documentation or other object browser for the arguments to the
Headers() and Footers() collection -- There are three: first page, even
page, and primary.

What small dot?

Doing this by copy and paste should be much the same; but the whole deal
sounds like a clumsy way to proceed. Might be simpler to create a new
document with the headers and footer already set up, then copy the *content*
of the old document.
 
R

rob

I already suggested that but the client wants to be able to click one button
and have the alternate header and footer pasted in. Would it be easier to
have the macro copy and paste the header and footer from a document already
made?

This is the code that I have so far for that Idea:
Sub CopyHeaderAndFooter()
Dim docSource As Document
Dim docDest As Document

Set docSource = Documents.Open("C:\MyFolder\sdoc.doc")
Set docDest = Documents.Open("C:\MyFolder\ddoc.doc")

docSource.Sections(1).Headers(wdHeaderFooterPrimary).Range.Copy
docDest.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paste
docSource.Sections(1).Footers(wdHeaderFooterPrimary).Range.Copy
docDest.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paste

docDest.Save
docSource.Close
docDest.Close
End Sub

Do you know how?

Many Thanks,

RP
 
R

rob

What Documentation and Where is the documentation on Headers Footers and
Collection? The small dot is the 5th Basic Greek Symbol; when you are in
word and clickt insert>symbol, you can find a pure black centered dot. And
that dot is what needs to go on the header and the footer in multiple places.
I know it doesn't make since to go to all this work just replace the header
but if you were in my shoes I think you would understand.

Many Thanks,

Ellsworth
 
J

Jezebel

First: If you're copying and pasting from one document to another, it's
easier to copy and paste the content (one range) from A to B than to copy
and paste the headers from B to A (one range for each header and footer
type). It would still be just a single macro, and thus a single click. If
you do this, when you copy the source range, do not copy the final paragraph
mark.

Set DocDest = ActiveDocument

ActiveDocument refers to the document that is active (and thus, by
definition, open) when the macro is called.


Still don't understand the issue with the center dot -- it's a character
like any other.
 
R

rob

Awesome on the Set DocDest = ActiveDocument but, when the sdoc.doc (source
document) is opened it becomes the active document right? If so, how do i
tell word to go back to the previous active document or not consider sdoc.doc
the active document?

Thanks for all your help Jezebel,

RP
 
R

rob

Also, what is the final paragraph mark?

Jezebel said:
First: If you're copying and pasting from one document to another, it's
easier to copy and paste the content (one range) from A to B than to copy
and paste the headers from B to A (one range for each header and footer
type). It would still be just a single macro, and thus a single click. If
you do this, when you copy the source range, do not copy the final paragraph
mark.


Set DocDest = ActiveDocument

ActiveDocument refers to the document that is active (and thus, by
definition, open) when the macro is called.


Still don't understand the issue with the center dot -- it's a character
like any other.
 
J

Jezebel

The whole point of using a document variable is that it's a constant
reference to one document --

Dim pDoc1 as Word.Document
Dim pDoc2 as Word.Document

Set pDoc1 = ActiveDocument
Set pDoc2 = Documents.Open(...)

pDoc1 and pDoc2 always point to the same documents, whatever happens to be
active.

The final paragraph mark is literally that: the last paragraph mark in the
document. Word stores information about sections and their headers and
footers in the paragraph mark. So if you copy that and paste it into your
new document, you'll overwrite the headers and footers in the new document.
 
J

Jean-Guy Marcil

rob was telling us:
rob nous racontait que :
I already suggested that but the client wants to be able to click one
button and have the alternate header and footer pasted in. Would it
be easier to have the macro copy and paste the header and footer from
a document already made?

I have not read the whole thread, so I may be off base...

But, if you want the options of changing a header according to a user
choice, why not simply store the different headers as Autotext entries in
the attached template and use that to insert whatever header/footer the user
wants? I have used this approach many times.

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

rob

I checked that out and it still requires that you have to go into the header
and footer and type (everything that client doesn't want to do....very lazy
clients). Thank you very much for the suggestions though, i didn't know
about that.

Many Thanks,

RP
 

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