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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect a page break

Status
Not open for further replies.

Jtorres13

Technical User
Nov 11, 2006
162
US
I have a one page letter template. Users open it and start filling out the text. If the letter remains as one page, I don't want a header or footer because it will print on letterhead paper. But, if the user types so much text that it flows to a second page OR if they insert a page break, I want the SECOND page header to have the creation date at the top of the header, then a paragraph mark, followed by the page number (2).

For the life of me, I can't figure out how to detect if the user added a page via typing or via page break.

I don't know how to monitor what's happening in the document real time. Help... I searched posts but didn't find any clues to follow
 
Hi Jtorres13,

If this is a Word document, you could just setup its template's page layout with the 'different first page' option, and add the header/footer detail to the second page. You don't need vba for any of this.


Cheers
[MS MVP - Word]
 
I've done that. But on the second page, I need to put a field from a database, txtRecipient. I have a
ActiveWindow.View.Seekview = wdPrimaryHeader.

But it only works if the second page is already visible, if I save the template with two pages. Users don't want to pages to start because most letters are only one page and they waste paper printing or have to delete it manually.

How do I make SeekView work for a page that is no there yet?
 
Hi Jtorres13,

You don't need to access the header (which is what ActiveWindow.View.Seekview = wdPrimaryHeader is trying to do) to update it. Instead, you can use something along the lines of:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = txtRecipient


Cheers
[MS MVP - Word]
 
Here is what I do when I have a single page (initial) template that may end up with more pages. It is based on an aspect of Word's document model that most people do not realize, or understand.

1. Headers and footers are child objects of Sections...NOT pages.

2. Word document sections always have SIX headers and footers. Always. Whether you use them or not. They are always there. Three headers, three footers.

Therefore....

...and I do this as a matter of course every time I make a Section....

I make my decisions as to what is what:

FirstPage content = whatever
EvenPage content = whatever
OddPage content = whatever

I actually make THREE pages and put the appropriate content into the headers and footers, then...delete the second and third pages.

Thus, there is ONE page. However, and this is the significant part, Word retains the content you previously put into the headers and footers of the other pages! Even though the pages are deleted, the headers and footers are still there, because - again - headers and footers are child objects of the Section.

Thus, if the document DOES go into a second page, the appropriate header is already there.

This will work for a field. Put the field into a blank second page header...then delete the second page. If ever a second page becomes...ummm, real...then that header (with the field, or whatever) will be there.

As a comment, I totally agree with macropod. If you are using VBA there is no need - EVER - to use .View when actioning headers or footers.

.View affects what is on screen, and doing so takes up resources and is, frankly, annoying. Headers and footers are objects, and thus can be actioned directly with VBA, with no need to View anything.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Ugh, I have so much more to learn! thank you all for trying. I will try your suggestions and see if I can get it in my head. i'm still finding hard to determine with object to use. For example, what you mention about not to use the Pages object but the Sections one. eeeeck, how do I decide which one to use is what takes me all day. As you may see, this is not what I do for a living, otherwise, taking weeks to solve a tiny problem would be a bad thing for my resume... :)
 
Basically, if you are talking about Word, it is not really meaningful to talk about pages. There is no real "page" in Word.

Everything you see in Word goes through the printer-driver. Everything. What you see as a "page" in Word is what Word determines would be a "page" if it was printed.

Thus, headers and footers do not belong to a page, they belong to the Section.

"For example, what you mention about not to use the Pages object but the Sections one. eeeeck, how do I decide which one to use is what takes me all day. "

There really is no which one. The key is truly thinking about what you want to do.

Again, taking macropod's comment re: accessing the header...he is quite correct (and I have expanded on it describing using a second page) in that you do not need VBA at all to do what you want.

What You Want To Do:

IF a second page is created (assumably by adding content to Page1 so it spills over into page2),

THEN have that second page header contain "X" (whatever).

ANSWER: Set the Section to have DifferentFirstPage, and then create the second page, including the header containing "X" (whatever). Delete the second page. That information about the second page is persistent, that is, it is retained even if there is no current second page.

That is macropod means by: "If this is a Word document, you could just setup its template's page layout with the 'different first page' option, and add the header/footer detail to the second page. You don't need vba for any of this. "

No VBA required.

However, if you DO need to use VBA to action a header (ANY header...or footer for that matter), because it is an object VBA can action instructions on it directly. It does not need to view anything.

As macropod put it:
Code:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
      .Range.Text = txtRecipient

badda bing badda boom

No View, no moving to the correct Section. It simply makes the text of the Primary header of Section 1 = the value of txtRecipient. Immediately.

Headers and Footers (in Word) are for many people a very confusing issue. I admit they seem to be, but once you get a handle on what they are, and where they fit in the Object Model (and the somewhat odd way Microsoft treats them), they actually are straightforward.

For example, Microsoft has a Document Model assumption - and Word has been like this from the start - that any new Section "should" have the same headers and footers as the previous Section. This is a conceptual assumption. One that I personally think is faulty.

The result of this assumption is that when you make a new section, all six headers and footers (and again ALL Sections always have six header/footers) are the same as the previous section. LinkToPrevious=True is the default.

This causes much grief for some. Again, I personally think it should be the other way around. A new Section headers and footers should be independent of the previous Section, unless you choose otherwise. However, that is not the way Word works. A new Section headers and footers are the same as the previous, unless you choose otherwise.

The reason I think this is...ummmm, incorrect, is because of the Object Model.

Section 1:
DifferentFirstPage = True......blank, no text in header
DifferentOddEven = True......"Chapter X" on far left, "Title" on far right. This is for EVEN pages.

The ODD pages have....."Title" on far left, "Chapter X" on far right.

OK?

Now you make a new Section, but you do NOT have DifferentFirstPage or DifferentOddEven. It is going to have the same header for all the pages in the new Section.

Which one do you think it will be?

Chapter X Title
or
Title Chapter


I am just messin' with your head.

If you want to get a serious handle on things, it is worth your time to take a good look at the Word Object Model.

Or just keep posting here with questions. There are a number of people who know what they are doing, and are willing to help. I can not calculate how much I have learned from members like macropod, or Tony Jollans, or anotherhiggins, or.........

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top