PDA

View Full Version : How to determine text position in table?



jumpjack
03-15-2012, 03:13 AM
I have a table several pages long.
Is it possibile to determine on which row, relative to a PAGE, the selection is located?

This code can only determine position relative to the beginning of the table:

Option Explicit

Sub SelectionInfo()
'
Dim iSelectionRowEnd As Integer
Dim iSelectionRowStart As Integer
Dim iSelectionColumnEnd As Integer
Dim iSelectionColumnStart As Integer
Dim lngStart As Long
Dim lngEnd As Long

' Check if Selection IS in a table
' if not, exit Sub after message
If Selection.Information(wdWithInTable) = False Then
MsgBox "Selection is not in a table. Exiting macro."
Else
lngStart = Selection.Range.Start
lngEnd = Selection.Range.End

' get the numbers for the END of the selection range
iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)

' collapse the selection range
Selection.Collapse Direction:=wdCollapseStart

' get the numbers for the END of the selection range
' now of course the START of the previous selection
iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)

' RESELECT the same range
Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart

' display the range of cells covered by the selection
MsgBox "The selection covers " & Selection.Cells.Count & " cells, from Cell(" & _
iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _
iSelectionRowEnd & "," & iSelectionColumnEnd & ")."
End If
End Sub

macropod
03-16-2012, 06:25 PM
I don't believe you can get the line number relative to the page, but you certainly can get the height relative to the margin or page boundary, if that's any help.

fumei
03-16-2012, 06:30 PM
Is it possibile to determine on which row, relative to a PAGE, the selection is located?
No, not relative to a page. That is because pages do not have any rows. But as Paul points out you can get a relative position to the margin or page boundary.

gmaxey
03-16-2012, 08:14 PM
What Gerry says is true but windows have panes and panes have pages and pages have rectangles and rectangles have lines:

If (and it is a big if) the page contains a single continous table then you might be able to use something like this:


Sub ScratchMacro()
Dim objPage As Page
Dim MyRect As Rectangle
Dim myRange As Range
Dim myLine As Line
Dim lngPageNumber As Long
Dim i As Long
lngPageNumber = Selection.Information(wdActiveEndPageNumber)
Set objPage = ActiveDocument.ActiveWindow.Panes(1).Pages(lngPageNumber)
Set MyRect = objPage.Rectangles(1)
For i = 1 To MyRect.Lines.Count
If Selection.InRange(MyRect.Lines(i).Range) Then
MsgBox i
End If
Next i
End Sub

jumpjack
03-17-2012, 12:46 AM
Panes, rectangles and lines?!?
Amazing!
It works like a charm, thanks gmaxey.

Even better, I can even select a single line inside the table row!

This code selects row CELL_ROW of cell (TBL_LINE,TBL_COLUMN) in the page:

objPage.Rectangles(1).Lines(TBL_LINE).Rectangles(TBL_COLUMN).Lines(CELL_ROW ).Range.Select

And, being this object a range, you can even select single characters (but notsinglw words), in the line.

Please note that "internal" number of lines and rows of the table of the page is increased by one w.r.t. real number.

I'll never stop learning something new about VBA! :-)

fumei
03-19-2012, 07:25 AM
Damn, it almost makes me want to "upgrade" to a version that has rectangles (I use 2002). BUT...not quite enough, as I do not have any serious need for anything like this. However, pretty cool I must say.