none
How to get the page setup line number using interop RRS feed

  • Question

  • I was wondering if there is a way to get what line number a sentence is on in  word. These are page setup line numbers.

    example

    5 and confirming that the world will not end in October

    I would like to get the line number 5

    Help would be greatley appreciated

    Monday, June 20, 2011 6:57 PM

Answers

  • Hi Chrys

    OK, I think I'm following you now (new week, new brain :-))

    There is nothing built into Word that's going to give you the entire range of numbers (and yes, I'm talking about the numbers that appear down the left). You have to work around it.

    So if you want the first and last numbers, what you'll need to do is "collapse" the Range (or the Selection) to its end-point (think of it like pressing the right-arrow key of a selection), then requery get_Information for the last number. Something like (pseudocode):

    Word.Range rng = wdApp.Selection.Range;
    int startLine = rng.get_Information
      (Word.WdInformation.wdFirstCharacterLineNumber);
    object collapseEnd = Word.WdDirection.wdCollapseEnd;
    int endLine = rng.get_Information
      (Word.WdInformation.wdFirstCharacterLineNumber);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Just Perdueit Tuesday, June 28, 2011 6:30 PM
    Tuesday, June 28, 2011 4:34 PM
    Moderator

All replies

  • I don't know if I exactly understand what you are asking for so this reply may not be helpful.

    You can get the line number of range or a selection with a command such as this:

    With Word.Selection (or a range object)

    lineNumber =.Information(wdFirstCharacterLineNumber)

    End With


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, June 20, 2011 8:03 PM
  • Yes this reply would be wonderful if I was looking for the actual word line number. But I need to get line number that have been created via page setup in word. For example

    This Document is not authorized for reproduction

    Written By Joe Programmer

     

    1 Java is not a procedural language

    2 Java is an object oriented language

    3 C# is based on java and c++

     

    I want to get line number 3 which in the word document would be like line 6 in word. I can't find a property or method to get the current line number from the left margin.

    Monday, June 20, 2011 10:29 PM
  • Selection.Information(wdFirstCharacterLineNumber)
    Cindy Meister, VSTO/Word MVP
    Wednesday, June 22, 2011 1:03 PM
    Moderator
  • Written By Pal Give Me the Darn Answer

    Reviewed By Chrys Johnson

    Published By AppDEv Solutions

     

    1

    2

    3

    4

    5

    6

     

    if the above was the document I need to find the numbers on the left there 1 through 6. If I were to use wdfirstcharacterline "Written By Pal Give Me the Darn Answer" would comeback as line 1.

     

    Any help would be appreciated

    Thursday, June 23, 2011 9:08 PM
  • I gave you the object model code in my previous response.
    Cindy Meister, VSTO/Word MVP
    Friday, June 24, 2011 6:43 AM
    Moderator
  • I don't think this is actually possible to do what I want with interop because the numbers along the side are not considered characters in a range. I'm fairly certain that (int)range.get_Information(wdFirstCharacterLineNumber); would give the same result as Selection.Information(wdFirstCharacterLineNumber); I'm also wondering what do I need to be able to get to Selection.Information extension method?
    Tuesday, June 28, 2011 2:23 PM
  • Hi Chrys

    OK, I think I'm following you now (new week, new brain :-))

    There is nothing built into Word that's going to give you the entire range of numbers (and yes, I'm talking about the numbers that appear down the left). You have to work around it.

    So if you want the first and last numbers, what you'll need to do is "collapse" the Range (or the Selection) to its end-point (think of it like pressing the right-arrow key of a selection), then requery get_Information for the last number. Something like (pseudocode):

    Word.Range rng = wdApp.Selection.Range;
    int startLine = rng.get_Information
      (Word.WdInformation.wdFirstCharacterLineNumber);
    object collapseEnd = Word.WdDirection.wdCollapseEnd;
    int endLine = rng.get_Information
      (Word.WdInformation.wdFirstCharacterLineNumber);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Just Perdueit Tuesday, June 28, 2011 6:30 PM
    Tuesday, June 28, 2011 4:34 PM
    Moderator
  • This is kinda what I did to fix the problem after I couldn't find property or method to use. I used sentences, words and collapsing the range like you pointed out to capture the line I needed and an offset.

     Luckily this only a problem on the first page of the documents I need to work with. Thanks for the help!

    Tuesday, June 28, 2011 6:35 PM