PDA

View Full Version : footer only on last page



neojapanese
05-22-2007, 02:57 AM
hello all.
i have some data thats being pumped in to msword so there is no way to know what the last page is going to me.

how can i make put the footer on the last page in word using vba.
it has to only show up on the last page.


this is my next question.
using vba/ado can i request data from sql server over the Net. we need to get data from remote locations.

thanks all

EricFletcher
05-22-2007, 05:30 AM
You can use a field code like this in the document footer:
{ IF { Page } = { NUMPAGES } "last page footer" "other pages footer" }

This will display a different footer on the last page. If you dont' need anything onthe other pages, just leave the second quoted sting empty (but inlcude the quotes). Note that you need to use Ctrl-F9 to get the braces; you can't just type them in.

neojapanese
05-22-2007, 05:46 AM
hi. the footer is not all the same fonts and some of the text in the footer is bold. what is the best way to deal with this with vba?

neojapanese
05-22-2007, 06:07 AM
with VBA how do i copy a foot from the first page to the last page then delete the footer fromt he first page?

fumei
05-22-2007, 11:09 AM
Why do you need to use VBA to do this? What is the reason you need to do this dynamically, using code?

EricFletcher
05-22-2007, 03:42 PM
I echo Gerry's question about why you need to do this with VBA. However, doing it within a template, a document or VBA would all be able to handle the formatting: just apply whatever formatting you need within the footer.

As far as copying the foot(er) from the first page to the last page, that isn't how footers work, so it isn't directly possible. There are 4 possible footers in any given section: all pages; or odd pages only, even pages only, and first page only -- the latter 3 controlled by the Layout tab of the Page Setup dialog. Note that footers are Section-dependent: a subsequent section will inherit the previous one's settings, but you can alter them if needed.

If you use my field code solution, you could put it in any of the first three footers. When it gets resolved during page layout, the field code will display the results of the IF logic; if you have the false condition set to a null string, nothing will be displayed except on the last page. (Of course, if you put it in an odd page footer and the last page was even, the logic wouldn't work.)

neojapanese
05-22-2007, 07:22 PM
the programmer b4 me make a tempelete file that has a footer already
i need that footer to show up on the last page only.
i put the code you told me b4 in but there is something in the code that overwrites it so it everworks.

i look all ove the code and there is nothing about footer in it. I cant see anyotherway that put footer data in. its getting the value for the footer. no matter what i do ..the footer never changes.

fumei
05-23-2007, 08:50 AM
Eric,
There are 4 possible footers in any given section: Incorrect.

There are always THREE footers in any given section.

HeaderFooterPrimary
HeaderFooterFirstPage
HeaderFooterEvenPages

There is NO odd page footer.

IF
DifferentFirstPage = False
DifferentOddEven = False

All headerfooters are HeaderFooterPrimary

IF
DifferentFirstPage = True
DifferentOddEven = False

First page = HeaderFooterFirstPage
All others = HeaderFooterPrimary

IF
DifferentFirstPage = True
DifferentOddEven = True

First page = HeaderFooterFirstPage
Even pages = HeaderFooterEvenPages
Odd pages = HeaderFooterPrimary

Primary is:

1. all pages
2. everything but first page
3. odd pages

neojapanese, can you post your code?

neojapanese
05-23-2007, 09:07 AM
help me understand this.

i have this word doc that has a footer.
i fill the page up with text then when i get to the second page .. a new footer shows up thats not the same as on the first page. I look all over the place and i cant find how that new footer is showing up replaceing the old one from the first page.

EricFletcher
05-23-2007, 03:22 PM
Right you are Gerry! I've never really considered looking at it from the point of view of the code but of course that defines it quite explicitly. I usually have to explain it to new users, and all, odd, even, and first is usually a lot easier to understand. I will use your logic in the future though.

neojapanese
05-24-2007, 05:17 AM
Its someone elses code. I just need some kind of sample code that lets me point to the footer of the last page and change it... think it could be done?

fumei
05-24-2007, 09:48 PM
So it is someone else's code. Yes....and the problem is????

I just need some kind of sample code that lets me point to the footer of the last page and change it... think it could be done?Yes, absolutely it can be done. I have done it lots of times.

Eric, it is crucial to think of it as the actual element values. For two reasons.

1. they persist. If you make a document DifferentOddEven, and put text into OddPages, and EvenPages....then change your mind, removing DifferentFirstPage, then what you put into Odd pages will, in fact, be Primary, and will become the header for all the pages.

If you change your mind back again, what you had previously put into Even, and Odd, will return. The values are stored in the Section break. IMPORTANT! the values are stored in the Section break following.

If there is no explicitly added section break, that final (undeletable) paragraph mark is ALSO a Section break, and contains the header/footer information.

2. The default of SameAsPrevious = True can really mess things up if you do not understand it. I will try to demonstrate.

A four page Document. One Section - ie. no added Section break.

Go into Page Setup and make DifferentFirstPage, and DifferentOddEven.

Go into headers and put the text as follow:

First page header: "First Page Header"
Even page header: "Even Page Header"
Odd page header: "Odd Page Header"

OK, so far so good.

You decide, no...I don't want different headers, this is going to be an introduction section. I want one header, and I am going to add a new Section.

You go into Page Setup, and uncheck DifferentFirstPage, and DifferentOddEven.

The header for all four pages becomes: "Odd Page Header" - as odd pages are, in fact Primary. DifferentFirstPage, DifferentOddEven unchecked...Primary is the header.

OK, a little funny, but OK.

Go to the end of the document, and add a new section. Now either:

A - add four new pages.

OR...

B - insert another file. Import (say) a four page document. Insert > File.

You DO want DifferentFirstPage, DifferentOddEven for this new Section.

So you go into Page Setup and make it so. What happens?

Section 1 - removed DifFirstPage, DifOddEven, header is "Odd Page Header" (Primary)

As soon as you make Section 2 DifferentFirstPage, DifferentOddEven....because SameAsPrevious is the default, Section 2 headers will automatically be:

First page header: "First Page Header"
Even page header: "Even Page Header"
Odd page header: "Odd Page Header"

The values of the previous headers will be carried over, even though DifferentFirstPage = False (unchecked) in that previous section. You can even test with:If ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) _
.Exists = False Then
MsgBox "Section 1 Different First Page is FALSE"
End If

Let me repeat this.

Section 1 no longer has DifferentFirstPage. It is unchecked in Page Setup. The code checks to see if it "exists". Run the code, and it will display:

"Section 1 Different First Page is FALSE"

YET, if you make a Section 2, and make it DifferentFirstPage (and do NOT change Section 1), then Section 2 FirstPage will be the text value of DifferentFirstPage of Section 1.

Test the code again, yup, Section 1 DifferentFirstPage is still false.

Is this a lie, a bug? No. The test is correct - there is no DifferentFirstPage for that Section. But there IS a value for the object wdDifferentFirstPage for that Section. You put a text value into it, and it persists. Forever.

The only way to remove that value is to:

A - manually turn DifferentFirstPage back on (check it in Page Setup), and then delete the text, and then uncheck DifferentFirstPage again

B - do it with code.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) _
.Range.Text = ""

You could be 17 new sections down the line, but if you make that section DifferentFirstPage...because of the default SameAsPrevious = True, that text from the previously input DifferentFirstPage will come back to haunt you.

Here is a trick I do.

When ever I make a new Section, I always immediately make THREE blank pages. I set the headers the way I want them. Then I delete the pages.

Even though the pages are deleted...the values I put into the headers are STILL THERE. They persist.

What happens if you don't?

You make a new section. You go into Page Setup and set DifferentOddEven. You add text. That text extends onto a new page. What will be the header?

If the previous Section also has DifferentOddEven, then that new page will have THAT previous Section header. It is a new page, and the default is....SameAsPrevious.

This is why I almost never, ever, use View > Header and Footer to work with headers. I do it all by code. I make a HeaderFooter object and I explicitly set them for the sections.

It is cleaner, and absolutely explicit.

A final note. The numeric value of the constants are:
HeaderFooterPrimary = 1
HeaderFooterFirstPage = 2
HeaderFooterEvenPages = 3

This can be important if you use a For var = loop through the collection.

Here is some sample code that can demostrate some of the above.Sub Yadda()
Dim strChapter As String
Dim strTitle As String
Dim oSection As Section
Dim var, var2
Dim j As Long
strChapter = "Chapter " ' note trailing space
strTitle = "My Novel"
j = 1
For Each oSection In ActiveDocument.Sections
For var = 1 To 3
With oSection
.Headers(var).LinkToPrevious = False
With .PageSetup
.OddAndEvenPagesHeaderFooter = True
.DifferentFirstPageHeaderFooter = True
End With
End With
Next
For var2 = 1 To 3
Select Case var2
Case 1
oSection.Headers(wdHeaderFooterFirstPage).Range _
.Text = ""
Case 2
With oSection.Headers(wdHeaderFooterEvenPages).Range
.Text = strTitle
.Style = "HeaderTitle"
End With
Case 3
With oSection.Headers(wdHeaderFooterPrimary).Range
.Text = strChapter & j
.Style = "HeaderChapter"
End With
j = j + 1
End Select
Next
Next
End Sub

What the above does:

Go through every Section (it does not matter how many), setting all three header objects to NOT be SameAsPrevious. Technically it is LinkToPrevious. Why it is displayed as SameAsPrevious in header View, I don't know.

It also explicitly sets each section to be DifferentFirstPage and DifferentOddEven.

Next, it goes through all three header objects (for each Section), and if it is:

First Page: header is blank (common for the first page of a Chapter)

Even Pages: the Title text, using HeaderTitle style (right aligned, TimesRoman, Bold, 14 pts)

Odd Pages: the Chapter, with an incrementing number. Eg Chapter 1, Chapter 2 etc., using HeaderChapter style (left aligned, Arial, 13 pts)

You can of course use any defined style you want.

The result is:

Chapter start page - no header
Next page: Title, right aligned
Next page: Chapter, left aligned

and so on, until the next Chapter (Section) then it repeats, except for the incremented Chapter number.

fumei
05-24-2007, 09:58 PM
Oh, and BTW; I have been using header interchangeably with footer.

Accurately speaking, there are SIX headerfooter objects for each section. They are always there, whether you use them, or not.

Three for the header.
Three for the footer.

Strange as it may seem, they use the same name, but are child objects of different Parents.

Section(3).Headers(wdHeaderFooterPrimary) is the Primary for Section 3 headers.

Section(3).Footers(wdHeaderFooterPrimary) is the Primary for Section 3 footers.

fumei
05-24-2007, 10:15 PM
To further enhance efficiency, suppose you have text for each chapter starting page. Eg.

(Chapter 1) "Murder at Sea"

(Chapter 2) "Initial Investigations"

(Chapter 3) "No one saw a thing"

If you bookmark that text, and assuming it is the first text of the section (being the Chapter title) then the index number for a range object set for the section can be used. Like this (skipping a lot of code):Dim r As Range
' yadda yadda....code
For Each oSection In ActiveDocument.Sections
Set r = oSection.Range
' yadda yadda...code
Case 3
With oSection.Headers(wdHeaderFooterPrimary).Range
.Text = r.Bookmarks(1).Range.Text
.Style = "HeaderChapter"
End With

If the Chapter title text is the first bookmark of the Range (set for each Section), then you can grab it and use it. You do NOT need to know its name. If it is the first in the Section, and a Range object is set for that section, then the index number is 1.

fumei
05-24-2007, 10:24 PM
neojapanese - I am not deliberately ignoring you.

Look up stuff in Help.

There are a lot of clues in what I have posted.

If you have different footers, then look in Page Setup. I mention that is where different footers are set.

You need to understand Sections. It is impossible to tell you what you want, as you do not describe your situation very well.

Eric's original field solution will work if you have one Section, and have nothing set as Different in Page Setup.

If that is not the case, well...describe it better. And post code. If you can not post code because, ummmm, it is someone else's, well...gee, that is unfortunate.