Results 1 to 6 of 6
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What Page (number) am I at? (Word97SR2 et al)

    I've just spent a happy time using the SEARCH function - thanks again guys! - and for the life of me can't find any references to postings that help me to determine what page I'm at in a document.

    The task:
    Loop through the document examining each paragraph.
    If the paragraph is the first paragraph on a page AND if the paragraph is bold, then
    Do something with it.

    My first thought was:
    <pre>Sub test()
    Dim prg As Paragraph
    For Each prg In ActiveDocument.Paragraphs
    If prg.pageNo <> intOldPageNo Then
    test for bold and maybe do something with it
    Else
    End If
    Next prg
    End Sub
    </pre>


    I have valid reasons for rchecking each paragraph; I want to test for other stuff along the way.

    There appears (Word97SR2) to be no way to determine the page number of the paragraph!

    Next I considered looping through the pages:
    <pre>Sub Test()
    Dim i As Integer
    For i = 1 To ActiveDocument.Pages.Count
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=Str(i)
    Next i
    End Sub
    </pre>

    But there isn't a property Pages of a document.

    I'm now greatly puzzled on two counts:
    1) the design of Word/VBA doesn't permit working with pages of a document
    2) there aren't scads of questions and answers about this in the VBA forum.

    All help would be appreciated.
    knowing me as i do, I've overlooked some basic facet of Word/VBA, and it is staring me in the face.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: What Page (number) am I at? (Word97SR2 et al)

    The .Information collection of the Range and Selection objects will return the page number (if it spans multiple pages, you need to determine that for yourself by setting a range to both ends). Your paragraph should be a range. Does that do it?

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: What Page (number) am I at? (Word97SR2 et al)

    You can also use the Information property to return the line number of a range.

    If the first character of your paragraph is on line 1 then you are the first paragraph on a page.
    If the last character of your paragraph is on a different page to the first character then a new page starts in the middle of the para. I don't know how you would want to treat this.

    StuartR

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What Page (number) am I at? (Word97SR2 et al)

    > If the first character of your paragraph is on line 1 then you are the first paragraph on a page.

    Thanks, Stuart.

    I probably won't do character-by-character analysis this time around, but it ought to be useful when I'm dropping in unexpectedly, when iterating through a series of .Find(s) for example. That would make for a real "WhatPageAmI?" function.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: What Page (number) am I at? (Word97SR2 et al)

    Maybe call a repaginate method (I assume, without looking, that there is one) now and again? That probably would slow things up considerably, but if you need accuracy... <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What Page (number) am I at? (Word97SR2 et al)

    >The .Information collection ... will return the page number

    JScer, thanks for this reply. When I go search my own library, I find several references to it. My memory is slipping. I'm glad that yours is fresh.

    <pre>Sub test()
    Dim prg As Paragraph
    Dim intOldPageNo As Integer
    For Each prg In ActiveDocument.Paragraphs
    If intOldPageNo <> prg.Range.Information(wdActiveEndPageNumber) Then
    intOldPageNo = prg.Range.Information(wdActiveEndPageNumber)
    prg.Range.Select
    Else
    End If
    Next prg
    End Sub
    </pre>


    The code below seems to select the paragraph BEFORE the next page, but I can tweak that. My guess is that it is tied up in the way Word determines exactly where starts the page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •