Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Word Macro Headers/Footers other?

Status
Not open for further replies.

alexander1113

Programmer
Dec 26, 2004
26
US
Hi,
Im trying to write a macro in Word in which it goes through every page. On each page it will write the order number as the header. But the problem I'm having is that when I write a header it writes them on all pages. I want to have different headers on each page because each page may have a different order number. I can't write them as footers because then that would totally dissassemble the page breaks which have been created. I have this so far:

For iPgNum = 1 To Selection.Information(wdNumberOfPagesInDocument)
sPgNum = CStr(iPgNum)

' Go to next page in iteration.
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, _
count:=sPgNum


objword = Selection.Words(1).Text
objword = Trim(objword)

Test = StrComp(objword, "New", 0)

If Test = 0 Then
MsgBox "YES " & objword

Set myCell = ActiveDocument.Tables(count).Cell(Row:=1, Column:=1)
myCell = Left$(myCell, Len(myCell) - 2)

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
Selection.HeaderFooter.Range.Text = myCell
record_count = 1
Else
record_count = record_count + 1
MsgBox record_count
End If

count = count + 3

Next
 
Headers are child objects of SECTIONS. Every Section has three headers (and footers), regardless of whether you have that section set as Different first page, Different Odd/Even, or not.

The three headers are:

wdHeaderFooterPrimary - this is the the header that is used if Different first page / Different Odd/Even are not set to ON. What it actually means is that they other two are EQUAL TO wdHeaderFooterPrimary. Primary is defined, from whatever page in the section you create a header. Once set it remains Primary. It can be changed from any page whether odd or even. HOWEVER, once the other two are set, it can only be changed from an odd page. This is because there is NO Odd page header. If Odd page header is set to ON, then Primary is used for Odd pages.

The other two are:

wdHeaderFooterFirstPage

and

wdHeaderFooterOddEven

If you set FirstPage, or OddEven to something different, then later unset then (make then OFF), Primary becomes the header for all the pages again.

Bottom line? If you want different headers for EACH page, you have to make different Sections for each page.

Or at least make sections that are only, and will remain only, three pages long. That way you could use Primary, FirstPage and OddEven...new section...repeat.

Note the collection that hold the headerfooter object is 1 based. That is wdheaderFooterPrimary is index 1, not 0.

Gerry
 
Hi Gerry,
Thanks for your reply. My question to you now is how do you assign each page a different section? So for example I want all of page 1 to be section 1, all of page 2 to be section 2, etc. Thanks again.

A.
 
Insert > Break, and choose. If you choose Next Page, then it will make a new page.

Your problem however, is you already HAVE the new pages. So it gets tricky. Properly your document would have been thought out and designed. You need a dfferent header for each page, then the document should be designed to handle that.

However, as I seriously doubt that this is the case, there are two questions back to you.

1. Do you have any hard page breaks?

2. Do you have tables or ANYTHING that you do NOT want to be separated by a section break? In other words, is your document truly distinquishable by page?

That being said, here is something that MAY help you get started.
Code:
Sub ChangePageBreaks()
Dim var
Selection.HomeKey Unit:=wdStory
For var = 1 To ActiveDocument.Range.ComputeStatistics(wdStatisticPages)
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1, Name:=""

    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Select Case Selection.Text
        Case Chr(12)  ' hard page break
            Selection.Delete
            Selection.InsertBreak Type:=wdSectionBreakNextPage
        Case Chr(13)
            MsgBox "This is paragragph mark"
        Case Else
            MsgBox "something else - probably text"
    End Select
Next
End Sub

This starts at the beginning of the document, go to each page, extends the selection one character, checks to see if it is a hard page break (Ctrl-Enter) and if it is, replaces it with a section page break. this makes individual sections for all the hard page breaks.

Now, of course if you simply have text that is longer than a single page, it makes a new page.

Now what do you do? Do you really want a new header for that page? These are DESIGN issues, and you are going to have to determine that. What ever you decide that you want to happen, most likely it is possible to make it happen.

Gerry
 
Hey Gerry,

Thanks for your reply. Now I already have page breaks, ones that I put in with a macro. The reason for this is is because, I have a number of invoices on a microsoft word document. So for example, you would have:

Invoice #1
.
.
.
.
Invoice #2 etc.

Now the reason i put page breaks is that, let's say for instance invoice #1 is 3 pages long. So invoice #2 starts on page 3 also. But we want invoice #2 to start on page 4 so that when we print they don't over lap. So i have inserted page breaks to separate them. So now if invoice #1 is 3 pages long, and there's space left over, invoice #2 won't start on page 3, but page 4, because my macro will make sure they don't overlap. Now the reason we want to place headers on top is, so that if invoice 1 is 20 pages long, then on the header it will have the invoice number, be it some number like 20032, along with the page, so on page 1, it would have "Order 20032 page 1 of 20", and on page 2 it would have "Order 20032 page 2 of 20", and so forth. So then invoice #2 starts on page 21 and it's 3 pages long, so then on page 21 it would have "Order 20033 page 1 of 3", page 22 it would have "Order 20033 page 2 of 3". This is my dilemma, the reason i need headers is because they don't ruin the page breaks. So this is my main problem.
 
Methinks it would be better to use one document per invoice!
Else, figure a way to put section breaks between invoices.
Note: If you can distinguish the beginning/end of an invoice, you can either:
1- replace the page break with a section break-next page
2 - add section break-continuous

have fun!
Jay
 
Hi Jay,
Thanks for your reply. The problem was that I can't use different pages for different invoices because they are being written from an asp page as one buffer. But I figured it out, I used Footnotes, which doesn't affect the page break, and in turn does what I need.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top