Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 01-21-2021, 04:26 PM
gmaxey gmaxey 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 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I've tinkered a bit with the code you provided. Note: No comprehensive test for accuracy. Still, here are is my result plus an alternate method:

Code:
Option Explicit
Sub WhereAmI()
    MsgBox "The cursor is at Document paragraph number: " & fcnSelectionParagraphNumber() & vbCr _
      & "Document line number: " & Selection.Information(wdFirstCharacterLineNumber) & vbCr _
      & "Paragraph line number: " & fcnSelectionParaLineNumber()
lbl_Exit:
  Exit Sub
End Sub
 
Function fcnSelectionParagraphNumber() As Long
Dim oRngSpan As Range
Dim lngPos As Long
  lngPos = ActiveDocument.Bookmarks("\startOfSel").Start
  Set oRngSpan = ActiveDocument.Range(Start:=0, End:=lngPos)
  If Selection.Start > 0 Then
    If Selection.Characters.First.Previous = Chr(13) Then
      fcnSelectionParagraphNumber = oRngSpan.Paragraphs.Count + 1
    Else
      fcnSelectionParagraphNumber = oRngSpan.Paragraphs.Count
    End If
  Else
    fcnSelectionParagraphNumber = 1
  End If
lbl_Exit:
  Exit Function
End Function
 
Function fcnSelectionParaLineNumber() As Long
Dim oRng As Range
Dim lngParaLine As Long, lngLine As Long
 Set oRng = ActiveDocument.Bookmarks("\Para").Range
 lngParaLine = oRng.Information(wdFirstCharacterLineNumber)
 Set oRng = ActiveDocument.Bookmarks("\Sel").Range
 lngLine = oRng.Information(wdFirstCharacterLineNumber)
 Select Case True
   Case lngParaLine = lngLine
     fcnSelectionParaLineNumber = 1
   Case Else
     fcnSelectionParaLineNumber = (lngLine - lngParaLine) + 1
  End Select
lbl_Exit:
  Exit Function
End Function


Sub GetLocation()
'Note this is just a example and with long complex documents, it could be a disaster.

Dim oPage As Page
Dim oRect As Rectangle
Dim oLine As Line
Dim lngPage As Long, lngRect As Long, lngLine As Long
Dim lngParCount As Long, lngLineCount As Long, lngPLCount As Long
Dim bResolved As Boolean
  bResolved = False
  
  For lngPage = 1 To ActiveDocument.ActiveWindow.Panes(1).Pages.Count
    Set oPage = ActiveDocument.ActiveWindow.Panes(1).Pages(lngPage)
    For lngRect = 1 To oPage.Rectangles.Count
      Set oRect = oPage.Rectangles(lngRect)
      On Error GoTo Err_Handler
      If oRect.Range.StoryType = wdMainTextStory Then
        If oRect.RectangleType = wdTextRectangle Then
          lngParCount = lngParCount + 1
          If Selection.InRange(oRect.Range) Then
            For lngLine = 1 To oRect.Lines.Count
              Set oLine = oRect.Lines(lngLine)
              lngPLCount = lngPLCount + 1
              If Selection.InRange(oLine.Range) Then
                lngLineCount = lngLineCount + lngPLCount
                bResolved = True
                Exit For
              End If
            Next lngLine
          Else
            lngLineCount = lngLineCount + oRect.Lines.Count
          End If
          If bResolved Then Exit For
        End If
      End If
NextRect:
    Next lngRect
      If bResolved Then Exit For
  Next lngPage
  MsgBox "The cursor is in Dococument paragraph number: " & lngParCount & vbCr _
       & "Document line number: " & lngLineCount & vbCr _
       & "Paragraph line number: " & lngPLCount
lbl_Exit:
  Exit Sub
Err_Handler:
  Resume NextRect
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-21-2021, 07:15 PM
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

GMaxey

Wonderful

Thanks for the GetLocation Option.
Why do you feel that Getlocation would be disaster with Long Complex document ?
I've not yet have any long Complex document

In all the three cases Even Blank Line/Paragraph between Two Paragraph is considered as Paragraph.

Would it be possible to count no(s) of paragraph without Blank Line/Paragraph ?

Thanks
SamD
Reply With Quote
  #4  
Old 01-21-2021, 08:49 PM
Guessed's Avatar
Guessed Guessed 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 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

SamD

Empty paragraphs are still paragraphs. It is generally considered bad practice to use empty paragraphs to increase the space between paragraphs containing text - that is what paragraph space above and below is for.

Altering the code to check the length of each paragraph (to identify empty paras) would slow the macro down considerably and be misleading.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 01-21-2021, 08:51 PM
Charles Kenyon Charles Kenyon 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 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by SamDsouza View Post
GMaxey

***
Would it be possible to count no(s) of paragraph without Blank Line/Paragraph ?
***
IMO, this is not practical. As far as Word and vba are concerned, when you press the Enter key you end one paragraph and start on the next. Word does not care whether you type anything first; it is a paragraph.

If you want, you could develop a count of empty paragraphs as an extra step and then subtract it from the total that Word sees.

My preference would be to use styles with inter-paragraph spacing set the way you want and not have the blank paragraphs. Empty paragraphs add to both the size and the complexity of a document.
Reply With Quote
  #6  
Old 01-21-2021, 09:37 PM
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

Thanks Charles

Now i understand Every line in word is a paragraph.

Quote:
My preference would be to use styles with inter-paragraph spacing set the way you want and not have the blank paragraphs. Empty paragraphs add to both the size and the complexity of a document
OK.
You , me and other VBA developers will know to set inter-paragraph spacing and styles

What about for the users ? Only way for them is to Select Document or Paragraph and check for style and spacing

So any type of indication thru VBA or any VBA syntax to know what style has been used for inter-paragraph and what spacing has been incorported.
Rather than Selecting document/Paragraph and checking for Space Size after each paragraph

SamD
Reply With Quote
  #7  
Old 01-22-2021, 08:58 AM
gmaxey gmaxey 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 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Sam,


"Every line in word is a paragraph" That is not true. A paragraph can have one line or hundreds. What is true is every press of the Enter key creates a new paragraph. If you press it twice and then start typing then you created two (one for space and one for your text).


As Andrew said, checking every paragraph to see if it is empty would just add to the time is take to process and return the user location.

That is one of the drawbacks of the GetLocation method. It is evaluating every page, every rectangle, every line up to the the users selection point. That is a laborious process. Nothing for a short letter or two or three page document but a big drag in a big document. I have a document that is 1510 pages long. I have a very robust PC and it still look close to a minute to report the cursor look on page 1500.


However, if you wanted to exclude empty paragraphs, and empty text rectangle in the document structure has a range length of 1. So you could modify to:


Note: This code has not be tested with documents containing tables or other graphical elements.


Code:
Sub GetLocation()
Dim oPage As Page
Dim oRect As Rectangle
Dim oLine As Line
Dim lngPage As Long, lngRect As Long, lngLine As Long
Dim lngParCount As Long, lngLineCount As Long, lngPLCount As Long
Dim bResolved As Boolean
  bResolved = False
  
  For lngPage = 1 To ActiveDocument.ActiveWindow.Panes(1).Pages.Count
    Set oPage = ActiveDocument.ActiveWindow.Panes(1).Pages(lngPage)
    For lngRect = 1 To oPage.Rectangles.Count
      Set oRect = oPage.Rectangles(lngRect)
      On Error GoTo Err_Handler
      If oRect.Range.StoryType = wdMainTextStory Then
        If oRect.RectangleType = wdTextRectangle Then
          If Len(oRect.Range) > 1 Then 'An empty TextRectangle as a range lenght = 1
            lngParCount = lngParCount + 1
            If Selection.InRange(oRect.Range) Then
              For lngLine = 1 To oRect.Lines.Count
                Set oLine = oRect.Lines(lngLine)
                lngPLCount = lngPLCount + 1
                If Selection.InRange(oLine.Range) Then
                  lngLineCount = lngLineCount + lngPLCount
                  bResolved = True
                  Exit For
                End If
              Next lngLine
            Else
              lngLineCount = lngLineCount + oRect.Lines.Count
            End If
          End If
          If bResolved Then Exit For
        End If
      End If
NextRect:
    Next lngRect
      If bResolved Then Exit For
  Next lngPage
  MsgBox "The cursor is in Dococument paragraph number: " & lngParCount & vbCr _
       & "Document line number: " & lngLineCount & vbCr _
       & "Paragraph line number: " & lngPLCount
lbl_Exit:
  Exit Sub
Err_Handler:
  Resume NextRect
End Sub

"So any type of indication thru VBA or any VBA syntax to know what style has been used for inter-paragraph and what spacing has been incorported.
Rather than Selecting document/Paragraph and checking for Space Size after each paragraph"


You could do a find and replace looking for ^13^13. Any found would indicate the user has used paragraphs for spacing.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 01-22-2021, 11:00 AM
Charles Kenyon Charles Kenyon 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 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by SamDsouza View Post
Thanks Charles

Now i understand Every line in word is a paragraph.

OK.
You , me and other VBA developers will know to set inter-paragraph spacing and styles

What about for the users ? Only way for them is to Select Document or Paragraph and check for style and spacing

***

SamD

Every line is not a paragraph.
In Word, every press of the Enter key creates a Word paragraph.


I suspect that every user, from time to time, uses the Enter key for spacing; I know that I do, in documents.
No well-formed template should.

Again, empty paragraphs add disproportionately to the size and complexity of documents. A large number of the problems reported on forums like this are the result of empty paragraphs that carry formatting. If text is directly formatted, each paragraph mark will carry up to fifty different formatting commands that Word has to interpret on the fly as it renders the document on the screen or print.

Any Word training should include use of styles and inter-paragraph spacing. This is not vba or expert-level material. When I hire staff to do word processing I do test and train. Word is not a typewriter, it is a complex program when used to produce complex documents. Basic Concepts of Microsoft Word - from Shauna Kelly

For simple documents, it is simple to use.
Reply With Quote
Reply

Thread Tools
Display Modes


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 06:48 AM.


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