Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Identifying page/line breaks (Word, any recent ver

    I need to write an application that identifies the page and line breaks in a Word document as it would be printed on the currently selected printer. I can't find anything in the Word object model for getting this type of information. Is it available? If not, what alternative ways might I get it?

    The only solution I can think of is to insert an XE field after each word, which is probably not feasible for long documents, and in any case would give me the page number only, not the line number.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Identifying page/line breaks (Word, any recent ver

    Look up the Information function in the Word VBA help; it applies to the Range object and to Selection.
    Information(wdFirstCharacterLineNumber) returns the line number (as displayed in the status bar) of the first character in the range/selection.
    Information(wdActiveEndPageNumber) returns the physical page number of the end of the range/selection.

    Perhaps you can also use the predefined bookmarks Line and Page. Look up predefined bookmarks in the Word VBA help.

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Identifying page/line breaks (Word, any recent

    Thank you for pointing out the Information function; it will help with the part of the problem that concerns getting line numbers. The other part of the problem still puzzles me.

    The Information function will tell me what position in output corresponds to a point in the file, but I want to know what point in the file corresponds to a position in output (the beginning of each line). It seems to me that using the Information function is like stuffing data into the wrong end of a funnel -- as the Word object model defines the funnel -- and the code is going to be rather inefficient. I will give it a try, though, and see what happens. Meanwhile, further suggestions are welcome.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Identifying page/line breaks (Word, any recent

    I don't understand what you want to accomplish, but you could write a loop, going down one line at a time until the end of the document is reached. The following does that in a simple document, but I haven't tested it in complicated documents.

    Sub LineEnds()
    Dim lngLineStarts() As Long
    Dim lngCurPos As Long
    Dim lngOldPos As Long
    Dim lngIndex As Long

    ' Initialize
    Selection.HomeKey Unit:=wdStory
    ReDim lngLineStarts(0)
    lngCurPos = Selection.Start
    lngLineStarts(0) = lngCurPos

    ' Loop
    Do
    lngOldPos = lngCurPos
    Selection.MoveDown Unit:=wdLine
    Selection.HomeKey Unit:=wdLine
    lngCurPos = Selection.Start
    If lngCurPos = lngOldPos Then
    Exit Do
    End If
    lngIndex = lngIndex + 1
    ReDim Preserve lngLineStarts(lngIndex)
    lngLineStarts(lngIndex) = lngCurPos
    Loop

    ' Do something with the information
    For lngIndex = 0 To UBound(lngLineStarts)
    Debug.Print lngIndex, lngLineStarts(lngIndex)
    Next lngIndex

    ' Clean up
    Erase lngLineStarts
    End Sub

  5. #5
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Identifying page/line breaks (Word, any recent

    I'm sorry I can't be specific about the application. Remember that I am interested in identifying lines and pages in printed output. The line numbers that I would get by counting lines in the manner you propose bear only a general resemblance to those. To give the most obvious example, your technique would count lines in tables, while Word line numbering does not. However my code works, it must produce the same information as the "line numbering" option in Page Layout, or it is not working right. I think I would have to work very hard to correct for all the situations where Unit=wdLine does things differently, and even then I could never be sure I had accounted for all of them.

    Returning to the Information function, I was dismayed to find that VBA does not recognize it. (I am using Word 2000.) It recognizes "Information" as a module name, but the object browser tells me that the module's members have nothing to do with my needs. The wdActiveEndPageNumber constants are undefined.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Identifying page/line breaks (Word, any recent

    Type Selection.Information in a module or in the Immediate window, and (with the insertion point in Information) press F1.

    Perhaps someone else has ideas how to emulate Word's line numbering in VBA.

  7. #7
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Identifying page/line breaks (Word, any recent

    A method of Selection, not a function. OK, I see it now.

    Weirdly, the constants you mentioned also work now, although they did not work before.

    I think I see a satisfactory way of dealing with my problem by combining the two techniques you suggested. Moving the selection down a line at a time will not let me count lines as Word counts them, but it will let me visit every line that _might_ be counted. Then I know where the line starts, and I can use Selection.Information to determine its "proper" line number. That will reduce the number of times I must iterate to process a typical file by a factor of more than 10, which reduces the problem from "foreseeably troublesome" to "not worth worrying about unless it happens."

Posting Permissions

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