View Single Post
 
Old 01-21-2021, 04:26 PM
gmaxey gmaxey is offline Windows 10 Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
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