Check page length and position on page RRS feed

  • Question

  • I am creating a Word document programatically. I want all paragraphs to fit neatly on one page without breaks.

    I need to check the current position of the cursor (i.e. which line it is on relative to the page) and then calculate whether there are enough lines left on the page to accommodate the next paragraph (I know the number of lines in each paragraph). If there is not enough space I will throw a page break.

    Can anyone help me with finding the line on the page and working out if there is enough space left for the next para?


    Friday, May 11, 2012 2:12 PM

All replies

  • The expression


    returns the line number of the first character in the selection. The expression


    returns the distance of the selection from the top of the page in points (1 inch = 72 points).

    ActiveDocument.PageSetup.PageHeight and ActiveDocument.PageSetup.BottomMargin can be used to determine the bottom of the usable area of the page.

    Regards, Hans Vogelaar

    • Proposed as answer by JosephFox Friday, May 11, 2012 11:44 PM
    Friday, May 11, 2012 2:40 PM
  • Hi Shalb

    Actually, Word has some built-in paragraph formatting capability that could save you all the coding and work involved in what you want to do...

    (Note: the following assumes Word 2007 or 2010. If you're using a different version, please inform us.)

    To get a feel for it, select a bunch of paragraphs that aren't breaking across pages the way you require.

    Click the dialog launcher at the bottom, right corner of the "Paragraphs" group in the Home tab of the ribbon.

    Go do the dialog tab "Line and Page Breaks". Make sure "Keep with Next" is NOT activated and "Keep lines together" is activated. OK.

    Does that do what you require? Then it should simply be a matter of applying this formatting to the paragraphs (or including it in the style definition of the paragraphs).

    Cindy Meister, VSTO/Word MVP

    Sunday, May 13, 2012 6:51 AM