Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-21-2021, 12:06 AM
SamDsouza SamDsouza is offline How to get respective/relative Line Number of a Paragraph ? Windows 10 How to get respective/relative Line Number of a Paragraph ? Office 2013
Advanced Beginner
How to get respective/relative Line Number of a Paragraph ?
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax for inserting blank line before inserting table and after a line or paragraph SamDsouza Word VBA 8 08-04-2019 11:10 PM
Data validation,force cell to be filed with number if respective cell is not empty nicholes Excel Programming 0 08-01-2015 09:08 AM
How to get respective/relative Line Number of a Paragraph ? Hanging relative to the number pmahesha Word 2 04-13-2015 12:16 PM
How to get respective/relative Line Number of a Paragraph ? How to create a table with a number of line depending a number entered by user Cellendhyll Word Tables 3 07-10-2014 05:49 AM
How to get respective/relative Line Number of a Paragraph ? How set up a 'paragraph number'? peytontodd Word 2 12-11-2012 06:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft