none
How to get the line number with count starting from the beginning of the first line in a Word document? RRS feed

  • Question

  • Hi, guys,

    Happy in entering the VSTO. I am a fresh from China, be familiar with VBA and some other fields, such like avionic embed software, modeling and software engineering process.

    I have a question when I count the current line number in a Word document from the beginning of the document. I use the

    "range.Information(wdFirstCharacterLineNumber)"

    to get the current line number, it does work but the current line number is counted from the beginning of each page, NOT the beginning of the document.

    So I have tried the

    "range.ComputeStatistics(wdStatisticLines)".

    I set a new range from the start of the document to the first character of the selection, and then call the "range.ComputeStatistics(wdStatisticLines)". But it returns the line numbers including lines in tables or orther things which are not counted in the displayed line number on the left margin. It's different from what we have seen on the left page margin while the Line Number attribute is set to Continuous in the Page Layout menu.

    And now I only can count the current line number of every page before the selection, and then make the summary of them. It's very slow and stupid, as the document has hundreds pages and there are thousands segments need to count the line number...

    Could you help me giving a breif and efficient method to get the

    Current Line number with count starting from the beginning of the first line in that document?

    Best regards,

    Charles



    Friday, June 29, 2012 9:21 AM

Answers

  • On 2012-07-02 10:40, Charles WangChao wrote:

    Hi, T.X.

    Thank you for your reply. But this function - set the line numbers as CONTINUOUS - is the first option item in the Word English version. And this requirement is even required by the British customer, whatever I tell him the combined Page Number and the Page Line Number can identify the unique line in the document.

    Have a good week,

    Charles


       
       
       
    I doubt that this is language-related. As you have noted, it's just that when getting line numbers using Word's object model, there is a difference compared to what is displayed in the user interface.

    Stefan Blom, Microsoft Word MVP
    Monday, July 9, 2012 9:50 AM

All replies

  • Hi Charles,

    As far as I know that there has no function to address your goal due to difference between our Chinese culture and English culture.

    Best Regards,

    T.X.


    征诛志异,三让两家王朝
    功同开辟,一桮万古江南

    Saturday, June 30, 2012 4:59 AM
  • Hi, T.X.

    Thank you for your reply. But this function - set the line numbers as CONTINUOUS - is the first option item in the Word English version. And this requirement is even required by the British customer, whatever I tell him the combined Page Number and the Page Line Number can identify the unique line in the document.

    Have a good week,

    Charles

    Monday, July 2, 2012 8:40 AM
  • Hi Charles,

    Following snippet seems work for you. Would you please try it?

    Sub test()
        Dim i As Integer
        Dim t As Table
        
        i = ActiveDocument.range.ComputeStatistics(wdStatisticLines)
        For Each t In ActiveDocument.Tables
            i = i - t.range.ComputeStatistics(wdStatisticLines)
        Next
        MsgBox i
    End Sub

    Best Regards,

    T.X.


    征诛志异,三让两家王朝
    功同开辟,一桮万古江南

    Monday, July 2, 2012 11:17 AM
  • Hi, T.X.

    It doesn't work, since there is some special lines counted into the Statistic Lines other than table lines. I don't know what kind of these lines, just find some cases such like a hidden line before the Content, lines occured by OLE item... 

    Thank you very much.

    Charles.

    Thursday, July 5, 2012 7:41 AM
  • Hi Charles

    I'd like to move this discussion to the Word for Developers forum since your question is really about how Word works; the VSTO technology isn't involved with the problem. What you're asking is not something you're going to get easily, I suspect. So it's probably a good idea to ask the specialists. Shall I move the discussion for you?


    Cindy Meister, VSTO/Word MVP

    Thursday, July 5, 2012 1:14 PM
    Moderator
  • Hi Cindy,

    Thank you very much. Could you please move this discussion to the Word for Developers for me? and please paste a link to the moved discussion here. I like the discussion of this question in any forum.

    Thanks a lot.

    Charles

    Monday, July 9, 2012 7:54 AM
  • Hi Charles

    If I move the discussion I can't paste a link to it "here", because the discussion will be there, not "here" :-) But you should get a link in the notification (as a matter of fact, any notification should route to the discussion, no matter which forum it's in).

    However, things can get lost, so here's the link to the Word forum. By default, discussions should be sorted by most recent to oldest, so you shouldn't have any difficulty finding it:
    http://social.msdn.microsoft.com/Forums/en-US/worddev/threads


    Cindy Meister, VSTO/Word MVP

    Monday, July 9, 2012 9:05 AM
    Moderator
  • On 2012-07-02 10:40, Charles WangChao wrote:

    Hi, T.X.

    Thank you for your reply. But this function - set the line numbers as CONTINUOUS - is the first option item in the Word English version. And this requirement is even required by the British customer, whatever I tell him the combined Page Number and the Page Line Number can identify the unique line in the document.

    Have a good week,

    Charles


       
       
       
    I doubt that this is language-related. As you have noted, it's just that when getting line numbers using Word's object model, there is a difference compared to what is displayed in the user interface.

    Stefan Blom, Microsoft Word MVP
    Monday, July 9, 2012 9:50 AM
  • Hi, Stefan,

    Thanks for your words. It is differece between the Word Object Model Line Number and the Page Displayed Line Number. So how can we get the line number displayed in the page margin?

    Hi, Cindy,

    Thank you for removing this post to the Word Developers forum. Have fun, :)

    And supprise! I can use the old link of this post to get it while it has been moved to the new forum. Thank you again, :)

    Charles


    Tuesday, July 10, 2012 3:24 AM