View Single Post
 
Old 01-21-2021, 12:06 AM
SamDsouza SamDsouza is offline Windows 10 Office 2013
Advanced Beginner
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default How to get respective/relative Line Number of a Paragraph ?

Hi
Anyone who could help in differentatiate between Relative line number and Absolute Linenumber.

Seems a mistake at the when running the following code from VBA Express : Word - Get Line and Paragraph Number
If one observes the Relative and Absolute line number which displays the same when macro is executed.

If absolute line number is the Exact position of the actual line number of the document where the cursor is on current line position then why relative line number is same as absolute. Should not relative line number be like for eg. Line 2 of Paragraph 3.

Ideally i would prefer to get Paragraph Number and Respective Line no of that Paragraph ir if iam on Paragrpah 3 then if the cursor is on line2 of Paragraph
Then it should Display Paragraph 3 Absolute/Relative Line Number 2. Although Absolute/Relative Line Number 10 is displayed with following result

with below code i get the following result
Quote:
Paragraph number: 3
Absolute line number: 10
Relative line number: 10
Rather than
Quote:
Absolute line number: 10
You are in line number: 2 of Paragraph 3
Code:
Option Explicit

Sub WhereAmI()
    MsgBox "Paragraph number: " & GetParNum(Selection.Range) & vbCrLf & _
    "Absolute line number: " & GetAbsoluteLineNum(Selection.Range) & vbCrLf & _
    "Relative line number: " & GetLineNum(Selection.Range) 
End Sub
 
Function GetParNum(r As Range) As Integer
    Dim rParagraphs As Range
    Dim CurPos As Long
     
    r.Select
    CurPos = ActiveDocument.Bookmarks("\startOfSel").Start
    Set rParagraphs = ActiveDocument.Range(Start:=0, End:=CurPos)
    GetParNum = rParagraphs.Paragraphs.Count
End Function
 
Function GetLineNum(r As Range) As Integer
     'relative to current page
    GetLineNum = r.Information(wdFirstCharacterLineNumber)
End Function
 
Function GetAbsoluteLineNum(r As Range) As Integer
    Dim i1 As Integer, i2 As Integer, Count As Integer, rTemp As Range
     
    r.Select
    Do
        i1 = Selection.Information(wdFirstCharacterLineNumber)
        Selection.GoTo what:=wdGoToLine, which:=wdGoToPrevious, Count:=1, Name:=""
         
        Count = Count + 1
        i2 = Selection.Information(wdFirstCharacterLineNumber)
    Loop Until i1 = i2
     
    r.Select
    GetAbsoluteLineNum = Count
End Function
SamD
Reply With Quote