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

Word 2007: Locate position on a page

Status
Not open for further replies.

BrianWen

Programmer
Jun 8, 2009
102
DK
Is there any way to get the position of the cursor or something on a given page in a Word Document. I have a userform, that based on checkboxes, insert fully texted chapters (heading 1 and some text afterwards). I want the script to read if there's more than perhaps 25% of the current page to go, if not, it should do a page break. This should result in a document, where a new chapter/section does start at the bottom of a page with only one line of text and then continues on the next page.

So, is there a property or something (on Selection class?) that tells me where on a page I currently am?

Thanks in advance!
 
Alternatively you may want to investigate a number of the Paragraph formatting options. Here's Word's own help file (this from Word 2007, but it is pretty much the the same for 2003 except that the Paragraph dialog is under the Format menu, not on the Page Layout tab). Given your expressed requirements you might particualrly want to look at the section entitled Place at least two lines of a paragraph at the top or bottom of a page
Word help said:
Prevent page breaks in the middle of a paragraph
Select the paragraph that you want to prevent from breaking onto two pages.
On the Page Layout tab, click the [blue]Paragraph[/blue] Dialog Box Launcher, and then click the [blue]Line and Page Breaks[/blue] tab.
Select the [blue]Keep lines together[/blue] check box.

Prevent page breaks between paragraphs
Select the paragraphs that you want to keep together on a single page.
On the Page Layout tab, click the [blue]Paragraph[/blue] Dialog Box Launcher, and then click the [blue]Line and Page Breaks[/blue] tab.
Select the [blue]Keep with next[/blue] check box.

Specify a page break before a paragraph
Click the paragraph that you want to follow the page break.
On the Page Layout tab, click the [blue]Paragraph[/blue] Dialog Box Launcher, and then click the [blue]Line and Page Breaks[/blue] tab.
Select the [blue]Page break before[/blue] check box.

Place at least two lines of a paragraph at the top or bottom of a page
A professional-looking document never ends a page with just one line of a new paragraph or begins a page with only the last line of a paragraph from the previous page. The last line of a paragraph by itself at the top of a page is known as a widow. The first line of a paragraph by itself at the bottom of a page is known as an orphan.

Select the paragraphs in which you want to prevent widows and orphans.
On the Page Layout tab, click the [blue]Paragraph[/blue] Dialog Box Launcher, and then click the [blue]Line and Page Breaks[/blue] tab.
Select the [blue]Widow/Orphan control[/blue] check box.
I'd further advise that you apply this through paragraph settings in Styles rather than through trying to set individual paragraph options manually or via code.
 
Hi strongm.

Thanks for the answer. I've looked at it, and as you may have suspected, it's already enabled (the widow/orphan option). I guess it's on by default?
Anyway, this is for a professional sales document and not a novel or something like that, meaning that in this context even two lines would not look good. It may even be as much as 50% free space I want to have if I'm not to do a page break. So two lines is not enough, and there doesn't seem to a way to further adjust the option.

But thanks for the reply, you pointed out a few things I didn't know existed. You don't know of a method or property that reads how long down on a page you are?
 


...this is for a professional sales document and not a novel ...
Then I would suggest using a tool that is designed for making brochures, and not one that is designed for writing novels.

Have you tried Publisher?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Publisher is not an option, also because the people who people who is using this, don't have the program. Also, I'd say that the document fits more a Word profile, than a Publisher profile, if you know what I mean.
 
Hi Brien,

You can format your headings with the 'keep together' and 'keep with next' attributes and, say, the first paragraph following the heading as 'keep together'. That way, if there isn't enough space for both the heading and the following paragraph on the page, they'll both be forced to the next page.



Cheers
[MS MVP - Word]
 
Good advice, especially if properly using Styles.

The problem though could be this:

"I want the script to read if there's more than perhaps 25% of the current page to go"

Word does NOT know this. If does not know if there is 25% "to go", or 50%, or 2%. At least not in advance. How could it? It can not read the future and has no idea whatsoever about what may (the "to go")....or may not...be put in after what is actually there.

Technically, you could possibly determine the spatial location from the top - but NOT from the bottom - of the last character, and them mathematically derive (with some major assumptions) what may possible be left. But Word itself does not know if there is 25% left "to go", or any other number.

What if there was 25% to go...but you changed the font to be 1/4 the size of the previous font? Well, you could fit all of the previous 75% of the page into that "25%" left. In which case, was it really "25% to go"?

You could possible work retroactively. AFTER the text is inserted, go back and check if a previous page has only X% used.

Have to ask though. Your userform:

"insert fully texted chapters (heading 1 and some text afterwards)"

What professional document would insert a fully texted chapter in the middle of a page - say if the page is 50% used already? Or even 10%. "Chapters" - and I may be misunderstanding your use of the word - normally are self starting. New chapter = new page.

Gerry
 
You have all pointed out some features and points I want to look into.

fumei, I understand what you're telling me, of course, and yuo guys obviously know more about Word than I do. However I don't see how it would be impossible for Word to determine how much of the current is used. It doesn't need to be in lines (because then your point would of course be true). It could also be in pixels, centimeters or simply percent. It can't be impossible for MS be include such function, but one could discuss the reasoning for it.

Regarding the fully texted chapters; Well, only some of them are actually fully texted and they are of varying size, of course. Some are very short and some very long.

But I'll see what solution I can come up with. Thanks for the replies and be all means, reply again if you have more to say!
 
Hi Brien,

At the most basic level, working out how far down the page you are is no more difficult than getting the current vertical position and expressing that as a percentage of the page height. A more useful measurement would be based on the height between the margins and that's fairly easy to obtain too. Check out:
expression.Information (wdVerticalPositionRelativeToPage)
expression.Information (wdVerticalPositionRelativeToTextBoundary)
and
ActiveDocument.Sections(expression.Information(wdActiveEndSectionNumber)).PageSetup.PageHeight
ActiveDocument.Sections(expression.Information(wdActiveEndSectionNumber)).PageSetup.TopMargin
ActiveDocument.Sections(expression.Information(wdActiveEndSectionNumber)).PageSetup.BottomMargin
where 'expression' returns a Range or Selection object.

Things start to get complicated, though, if the page footer is anything other than the minimum and/or you've got footnotes on the page.


Cheers
[MS MVP - Word]
 
I don't know if they'll use footnotes... they might. But I will look at the properties and see if they can be useful. Thanks for pointing them out.
 
That might actually work just fine, from what I can see. I've tried it out a little. But you're saying footnotes will make it go wrong? - I presume it's simply because the footnotes is placed at the bottom of a page, but not in the footer, so we can't get a height of it and subtract that? - Am I right?
 
macropod said:
Things start to get complicated, though, if the page footer is anything other than the minimum and/or you've got footnotes on the page.

That my friend is putting it mildly.

BrianWen, as it has being pointed out, yes it is possible to do some calculations to determine position, but remember, these derived numbers are for the Selection point.

"However I don't see how it would be impossible for Word to determine how much of the current is used. "

As macropod shows, yes, it is possible. But you have to do it. There is no built-in existing Word, or VBA, function that does it.

Gerry
 
I've fiddled with some more and I've come to a problem I hope some of you have a solution for:

The page break works just as intended but only on the first page. See, the script inserts maybe 10 pages of text, divided up in chapters. As far as I can understand from what I see, it stops to update/register the correct position relative to x (either of these: expression.Information (wdVerticalPositionRelativeToPage) expression.Information(wdVerticalPositionRelativeToTextBoundary)). So that number increases for each section of the document, that gets included, as intended, but once the script has included enough text to continue off screen, those properties keeps reporting back the same number, not regarding the actual position of the Selection :(

Any good ideas?
 
I'd put the document in print preview mode before to be sure the counters are accurate.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Doesn't seem to make any difference. It counts wrong even when the script runs while the document is in Print Preview. I presume that's what you meant...
 
Well, the Print Preview didn't work, however I found out I could zoom out to 10% (so it can display all the pages), put in the text and then zoom in again...
 
Brian,

I admire your persistence, but I really do think there are smarter ways - specifically, using Styles with paragraph formats as I described some time ago.

As for your page break problem, it'd be easier to diagnose with some code to work with.


Cheers
[MS MVP - Word]
 
macropod: Thanks for the answer. I'm not sure, what you reference? - Do you mean the "Keep Together" and those? Those aren't providing the functionality I seek (at least not how I see it, I might be wrong).

As of now, I've created a small pagebreak function I can call. It checks, with the properties you provided, how far down the page the selection is and breaks the page if it's too long...
 
I think a real question comes from:

" See, the script inserts maybe 10 pages of text, divided up in chapters. "

The question is, is this text properly using Styles? Yes or no? And, it sounds like this chunk of text is being inserted as a chunk, rather than separate chunks (heading and text following, each inserted separately).

If it IS being inserted as ONE chunk, then I can not see how you are getting those gaps you seem to be getting.

Gerry
 
The headline is inserted as a Heading 1, the script then makes two paragraphs and inserts some text (Normal).
So two seperately insertions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top