PDA

View Full Version : how insert header and footer in specific page in word document with VBA



rghnni44
07-25-2012, 10:49 AM
Hello Forum !
I need your help to modify the below code in order to insert header and footer in a specific page in the word document.
the below code inserts header and footer in all the pages. I want to add only this in the activate page where the cursor is activated.

thanks in advance for your help :thumb



Sub addheader()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
.Shapes.AddPicture FileName:="C:\Users\Desktop\header.png", LinkToFile:=False, SaveWithDocument:=True
End With
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "Gauche" & vbTab & "Centre" & vbTab & "Centre" & vbTab & "Gauche"
End Sub

fumei
07-25-2012, 11:46 AM
Headers and footers are part of Sections. A header can not be put on a specific page - whether the cursor is there or not. At least it can not unless that page is in its own Section. This has consequences, particularly if the page is question does not have hard page breaks between it and the previous (and next) page(s).

rghnni44
07-25-2012, 01:01 PM
Thanks for your quick reply , how can you help me to improve my code ?

rghnni44
07-25-2012, 01:54 PM
hi all ! need help ! in order to make life easier I suggest to copy a whole letterhead ( including header, footer and text) in a new document or existing one. how we can do that with VBA.. thanks

macropod
07-25-2012, 03:50 PM
Hi rghnni44,

It's really not a matter of 'improving' the code.

As fumei says, Headers & footers apply to whole Sections. Unless you're tring to add the header content to the first page of a Section (which you've already defined as having a different first page layout), your choices are limited to having the content appear on all pages, all odd pages or all even pages. Anything else requires either complex field coding or the insertion of a Section break before & after the page concerned.

fumei
07-25-2012, 03:57 PM
PLease keep to one subject. If you need something else, please post a new thread.

Back to the header on a specific page. What makes this page - as opposed to other pages - need a header. And why do you need to do it with code?

Again to have a header different from other Sections, you need to make a new Section. So, go to the top of the page in question, and then insert break (Next page). Then, if you do NOT want the header to continue on the page after, go to the end of the page in question and make another Section break. Now this makes the page in question in its own Section.

Adjust the header. Remember though that unless you change it, whatever you put in as header will ALSO be the header for the previous Section and the following one. You change it by switching off Same as Previous.

Sound complicated? Not really. But you do have to take care with headers.

fumei
07-25-2012, 03:59 PM
It may help if you answered the question to WHY you want a header on a specific page, and (we assume) ONLY that page.

How about making a hard page break (before the page in question) and simply putting the graphic at the start of the page?

rghnni44
07-25-2012, 04:46 PM
thanks for your replies.
the file i have attached it is the letterhead looks like. It has a picture at the top and a set of 4 text blocks at the bottom. What i need is,

I open microsoft word ( new document or existing document). I can click on a macro. when i click on the macro, this letterhead appears on that page. (could be any page on the document, or one or more pages)
and putting the letterhead on this page should not effect margins on other pages. that s all.

thanks again.

macropod
07-25-2012, 06:41 PM
There is no file attached to your last post. In any event, it really doesn't matter what the letterhead looks like - you cannot simply put the letterhead on 'any page on the document, or one or more pages'. Word has very specific rules, as per my previous post (and fumei's), about how a document must be set up for the use of headers (and footers). If you study those, you'll see that you'll need to use Section breaks to manage the letterhead requirement. This is no different than if you were doing it manually. The code for inserting a Section break is quite straightforward. What isn't is all the testing you'll need to add to establish whether you're working with the first page of an existing Section and whether the page layout needs changing to the 'different first page' configuration. This has nothing to do with margins on any page.

rghnni44
07-25-2012, 06:51 PM
thanks macropod ! sorry here is the letterhead attached.
can you pls help me as this is very urgent !! thanks again macropod.

macropod
07-25-2012, 06:57 PM
Do you ralize that, with the document you posted, all you need to do to get the 'leterhead' to appear on a new page is to insert a 'Next Page' Section break? No vba code is required, as the document already uses a 'different first page' layout.

rghnni44
07-25-2012, 07:08 PM
this is only the sample of the letterhead , but this letterhead can be the first or any page in the document... in the second page for example i dont want that the letterhead appears , it can appears in the 3rd or the last page for example... hope all is clear

macropod
07-25-2012, 07:19 PM
You don't seem to be paying attention.

If the document already has the layout of your attachment, then all you need to do is to insert a 'Next Page' Section break to create a new page with a letterhead wherever you want it to be.

If you're using a document that doesn't have the same setup as your attachment, then you need to insert a 'Next Page' Section break, format the new Section's layout with a 'different first page' layout, unlink the new Section's header from the previous section, then add the letterhead. BUT, once you've done that once in the document, you may need to do nothing more from then on than insert a 'Next Page' Section break to insert additional letterhead pages.

Finally, I must say that it is rather odd to have letterhead pages interspersed throughout a document. Letterheads are usually reserved for the first page of a document. It seems you're trying to use one file as a holder for multiple logical documents.

fumei
07-25-2012, 07:55 PM
Does this mean you are no longer interested in your original subject:

how insert header and footer in specific page in word document with VBA

rghnni44
07-25-2012, 11:26 PM
no I m always interested ... awaiting for your help... some suggestions please

fumei
07-26-2012, 04:01 PM
... some suggestions please

There ARE suggestions already posted.
Again to have a header different from other Sections, you need to make a new Section. So, go to the top of the page in question, and then insert break (Next page). Then, if you do NOT want the header to continue on the page after, go to the end of the page in question and make another Section break. Now this makes the page in question in its own Section.

Adjust the header. Remember though that unless you change it, whatever you put in as header will ALSO be the header for the previous Section and the following one. You change it by switching off Same as Previous.


macropod has suggested essentially the same thing. So. Have you done any yet?

rghnni44
07-26-2012, 04:53 PM
Hello fumei, thanks !
I undrestand the above but I want to automate this with VBA please , when I go to the page in question I run the macro , it will insert break Next page and insert another section break as i don t want the header and footer to continue on the page after. many thanks !!

macropod
07-26-2012, 08:32 PM
Have you tried recording a macro to implement these steps? The macro recorder will give you most of the code you'll need.

rghnni44
07-26-2012, 08:46 PM
no idea how can i do that .. can you pls send me the code pls ?

rghnni44
07-26-2012, 09:14 PM
i tried this :



Sub Macro1()
'
' Macro1 Macro
'
'
Application.Run MacroName:="addheader"
ActiveWindow.ActivePane.VerticalPercentScrolled = 46
Selection.InsertBreak Type:=wdPageBreak
ActiveWindow.ActivePane.SmallScroll Down:=-5
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.SmallScroll Down:=-87
ActiveWindow.ActivePane.VerticalPercentScrolled = 74
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveWindow.ActivePane.VerticalPercentScrolled = 73
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
End Sub

fumei
07-27-2012, 06:04 PM
I'm sorry, but are you paying any attention at all?????

You are asking about doing something (a header with a specific page).

We (both Paul and I) have told you what needs to be done. You need to make that page a new Section (by making a Section break before and after it).

You say you understand. "I undrestand the above"

Clearly you do not.

Try recording a macro that does what you want. Your code above does not do this.

So...try recording a macro that does what you want. We will help you tweak it if you need that. It probably will need adjusting, but we are willing to help with that.

Record a macro that puts a Section break at the start of the page in question, and a Section break after it. Start there.

"no idea how can i do that .. can you pls send me the code pls ?" I will not do so. I will help you fix or adjust what you come up with. What are you saying you have no idea how to do? Record a macro? Make a Section break?

fumei
07-27-2012, 06:19 PM
I have to say that if you tried to do what you want to any given document, it would probably take LESS time than what has been spent on this thread. I am not sure why you seem to be fixated on a VBA solution. How many times are you going to be using this? Unless it is a lot, you can definitely do it faster by...just doing it.

rghnni44
07-28-2012, 01:42 AM
sorry I understand all but I couldnt record the macro correctly.
###puts a Section break at the start of the page in question, and a Section break after it. Start there####

macropod
07-28-2012, 03:30 AM
Surely you can record a macro to create a Section break, give the new Section a 'different first page' layout and unlink the new Section's header & footer from the previous Section. I can!!

Unlike fumei, I don't think it's necessary to insert another Section break after the new page as well - a 'different first page' layout should be sufficient.

From then on, it's simply a matter of ensuring the new header & footer are empty then insert the content you require.

Whilst the macro recorder generates a lot of code you don't need, it also provides you with all the essentials.

Here's all you should need:
Sub CreateLetterheadPage()
With Selection
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.DifferentFirstPageHeaderFooter = True
With .Sections.First.Headers(wdHeaderFooterFirstPage)
.LinkToPrevious = False
.Range.Delete
.Shapes.AddPicture FileName:="C:\Users\Desktop\header.png", LinkToFile:=False, SaveWithDocument:=True
End With
With .Sections.First.Footers(wdHeaderFooterFirstPage)
.LinkToPrevious = False
.Range.Delete
.Range.Text = "Gauche" & vbTab & "Centre" & vbTab & "Centre" & vbTab & "Gauche"
End With
End With
End Sub

rghnni44
07-28-2012, 09:36 AM
thanks so much indeed, the macro runs perfectly but this is not working for the first page if i want to insert header and footer , we can make this in a another macro. thanks
Further to that in footer is it possible to add 4 text bloc. thanks so much again ! so much appreciated.

fumei
07-28-2012, 04:05 PM
Frankly Paul, I am sorry you gave him that.

The reason I suggested making the second section break was that I had no confidence that NOT isloating the Section, as its own, was not going to come back to bite again. I will let you handle it from here.

macropod
07-28-2012, 06:01 PM
Hi Gerry,

I was reluctant to, as the required additional code can almost entirely be gleaned from what the macro recorder produces. However, I felt it would require less effort on my part to provide the code (which took maybe 5 minutes) than to keep telling the OP what was needed and put up with the bleating responses about it.

That said, until I see evidence that the OP is going to make a meaningful effort re the first page issue, there'll be no more from me.

rghnni44: I have no idea what you mean by "add 4 text bloc". The code I (reluctantly) provided was based solely on what you'd already specified/supplied. If you want anything more, I suggest you invest some meaningful effort. Who knows, you might surprise yourself and realise it's not at all difficult. This is not simply a free coding forum for those who are unwilling to help themselves.

fumei
07-28-2012, 06:20 PM
Oh and having a single page ("isolated") section dispenses with the need for a DifferentFirstPage. Although, admitedly, it is six of one, half dozen of another...

macropod
07-28-2012, 06:24 PM
IMHO the 'different first page' approach is more flexible, as it allows text from the letterhead page to flow over to subsequent pages, if needed. A pair of Sections breaks without the 'different first page' setup won't do that withoout also putting the letterhead on the next page.

rghnni44
07-28-2012, 07:30 PM
thanks so much for help !
this code will take care about the 1st page :


Sub InsertFirstPage()
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.AddPictu re FileName:="C:\Users\desktop\header.png", LinkToFile:=False, SaveWithDocument:=True
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text = "Text here" & vbTab & "Texte here" & vbTab & "Texte here"

End Sub


Regarding the 4 BLOC text in footer , pls see take a look at my first attached sample to know what i mean.

Thanks

fumei
07-28-2012, 10:39 PM
I, for one, can not open the .dotm file. I finally decided that I despise 2010 so much that I uninstalled it. So I do not know what you are talking about re: 4 BLOC text.

Four blocks of text?

I do want to point out that the last code:ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.AddPictu re
etc etc
affects Section 1. This is NOT the same thing as your subject, nor what Paul and I have been posting. If you make a new Section break at the start of a specific page, that new Section will NOT - repeat not - be Section 1.

Again, try recording a macro that does what you want.

macropod
07-29-2012, 05:57 PM
Hi Gerry,

The file's actually in docm format, which you probably can open. Be that as it may, the footer content in that file is a dog's breakfast - it consists of content in four separate textboxes. I, for one, wouldn't even bother trying to replicate such a poor setup in code (or otherwise).

rghnni44: You need to learn something about proper document design before you try to do anything more. For starters, I'd suggest designing the footer as a borderless, four-column Word table. You should then consider whether you want to create such a table by code (all of which you can do, using the macro recorder) or, perhaps, save the completed letterhead as a template from which both the completed header and footer can simply be copied and pasted into the active document (again, the macro recorder can provide all (or at least most) of the code you'll need.

fumei
07-29-2012, 09:23 PM
Sorry, I meant to write .docm, but no, I can NOT open it to see it. I just tried again, and no go.

Can't say I am surprised it is a 'dogs breakfast'.

So the "4 bloc text" is four separate textboxes? Excuse me while I ROFL...as they say.

macropod
07-29-2012, 10:03 PM
Hi Gerry,

Have you installed the docx/m compatability pack? See: http://www.microsoft.com/en-us/download/details-b.aspx?id=3

rghnni44
07-30-2012, 09:39 AM
hi all, herewith i attach the file .doc
thanks

fumei
07-30-2012, 12:18 PM
That's nice...but so what? I can see the document, and I have to agree with macropod. It is a mess, and I would not try and ocd anything nlike that.

So you show the document, but what do you want from us? We have helped you quite well, but you do nothing with our help. You have a number of suggestions but seem to expect some magic solution where you do nothing.

rghnni44
07-30-2012, 03:37 PM
i m not an expert like you guys !!!

fumei
07-30-2012, 03:44 PM
Clearly. And I for one have stated that I would help you adjust/fix code you come up with, but you do not appear to be even trying very hard at all.

You do not need to be an "expert". What you say you want does not require an expert. It requires paying attention and actually trying. It requires be absolutely clear about what you want to do (which you are not). It requires getting at least a minimal understanding of how Word works (which you do not have).

For example, you started off by asking to get a header to a specific page. Is this page the very first page? Yes? No? If no, then do you understand what i said when I stated that any new Sectiobn would NOT be Section 1 - and therefore any code that actions Section 1 will not do what you want.

If yes, then simply making DifferentFirstPage will work. Do you understand what macropod was writing about using a template?

rghnni44
07-30-2012, 03:48 PM
so now i couldnt insert 4 bloc text in footer as indicated in my file.

fumei
07-30-2012, 03:53 PM
"as indicated in my file"? What does that mean? HOW did you try to insert things? You are telling us nothing.

rghnni44
07-30-2012, 04:19 PM
have you checked my last attached file? in footer there is 4 bloc text.

fumei
07-30-2012, 04:38 PM
Yes I did. Your point?

rghnni44
07-30-2012, 04:46 PM
let the macro inserts a footer like the footer indicated in my file

fumei
07-30-2012, 05:50 PM
See macropod's post (#32). And you STILL are not really paying attention.

If I were you I would forget about doing anything with code until you have a minimal grasp of Word fundamentals. Unless you are still trying to make a header for a specific page (NOT page #1, in which case you need a new section break as has been mentioned a number of times so far), working with a template - which has ALSO been mentioned a number of times - would the best route.

I have no idea why you seem to be insisting on a VBA solution (when you do not really try to work one out). You do not have the skills to use VBA, and unless you have many many situations that you need one for, there is no need for a VBA solution.

We are not going to give you one, for the main reason you have not been very clear at all, nor shown any effort to work on a VBA solution.

Good luck.

rghnni44
07-30-2012, 06:51 PM
Ok thanks ! bye !

macropod
07-30-2012, 10:44 PM
i m not an expert like you guys !!!
you don't have to be an expert. I have already suggested what to do, using the macro recorder. If you can't do at least that much, it's just laziness, not a lack of expertise.

rghnni44
08-09-2012, 01:56 PM
i have found a solution how can i mark subject is resolved thx

fumei
08-14-2012, 09:19 PM
Please post the solution you say you found.