PDA

View Full Version : Figuring Out a Selection's "Row" or "Col"



MWE
03-28-2006, 07:35 PM
Assume that the selection is a row for a given table. How can I determine which row? If the selection is a col for the table, how can I determine which col? Similar for an individual cell in a table?

Thanks

fumei
03-28-2006, 08:03 PM
Selection.Information(wdEndOfRangeRowNumber)gives the row number....and....Selection.Information(wdEndOfRangeColumnNumber)gives the column number.

If the Selection is the entire row, this gives the row number of course. But this also works if only a cell in the row is selected. Ditto for columns. So extrapolating:Cell(Selection.Information(wdEndOfRangeRowNumber), _
Selection.Information(wdEndOfRangeColumnNumber))If only a single cell is selected, this will explicitly return the selected Cell(x,y) parameters.

Remember it is always a good idea to test using:Selection.Information(wdWithinTable) to make sure that the Selection IS in a table, otherwise, there is no error. The value returned is simply -1.

It may be important to note that the cell (or row, or column) does NOT have to be selected. The code returns the row/column number the selection point is in. If a selection covers multiple rows (or columns) note that the it is the END of the selection range that is returned.

TonyJollans
03-29-2006, 02:36 AM
If the selection is in a table you can also useSelection.cells(1).rowIndex
' and
Selection.Cells(1).ColumnIndex

MWE
03-29-2006, 07:04 AM
Selection.Information(wdEndOfRangeRowNumber)gives the row number....and....Selection.Information(wdEndOfRangeColumnNumber)gives the column number.

If the Selection is the entire row, this gives the row number of course. But this also works if only a cell in the row is selected. Ditto for columns. So extrapolating:Cell(Selection.Information(wdEndOfRangeRowNumber), _
Selection.Information(wdEndOfRangeColumnNumber))If only a single cell is selected, this will explicitly return the selected Cell(x,y) parameters.

Remember it is always a good idea to test using:Selection.Information(wdWithinTable) to make sure that the Selection IS in a table, otherwise, there is no error. The value returned is simply -1.

It may be important to note that the cell (or row, or column) does NOT have to be selected. The code returns the row/column number the selection point is in. If a selection covers multiple rows (or columns) note that the it is the END of the selection range that is returned.thanks. your reply was quite useful.

fumei
03-29-2006, 11:28 AM
And if you combine my suggestion AND Tony's suggestion you can get row/column info on the full range of a Selection.

Tony's returns the START of the Selection.
Mine returns the END of the Selection.

Assuming of course that the Selection spans multiple rows and/or columns.

MWE
04-07-2006, 06:18 PM
And if you combine my suggestion AND Tony's suggestion you can get row/column info on the full range of a Selection.

Tony's returns the START of the Selection.
Mine returns the END of the Selection.

Assuming of course that the Selection spans multiple rows and/or columns.thanks again.

I wish to operate on the cells adjacent to the cell containing the current selection (or on the range of cells). With respect to what "object" is the row and col info provided?

fumei
04-07-2006, 06:43 PM
Define "adjacent". The cell above (if there is one)? It is adjacent. The cell below (if there is one). It is adjacent. The cell to the left...if there is one...to the right...if there is one?

In any case, as I stated, using either my code, or Tony's, or a combination, you can get the (x,y) of the cell the Selection is in. First of all, though, you had better make sure that the Selection is IN a cell, not spanning cells. Easy enough. Explicitly collapse the Selection.

Now you can get the (x,y) of the cell it is in. Obviously you need to do some checking on the total x and y of the table. If the Selection is in Cell(4, 2) - fourth row, second column - you do not want to perform action on Cell(5, 2) is there isn't a Row 5! To answer your question:
With respect to what "object" is the row and col info provided?The Selection object. What else would return:Selection.cells(1).rowIndex
or
Selection.Information(wdEndOfRangeRowNumber)

MWE
04-07-2006, 07:21 PM
Define "adjacent". The cell above (if there is one)? It is adjacent. The cell below (if there is one). It is adjacent. The cell to the left...if there is one...to the right...if there is one?

In any case, as I stated, using either my code, or Tony's, or a combination, you can get the (x,y) of the cell the Selection is in. First of all, though, you had better make sure that the Selection is IN a cell, not spanning cells. Easy enough. Explicitly collapse the Selection.

Now you can get the (x,y) of the cell it is in. Obviously you need to do some checking on the total x and y of the table. If the Selection is in Cell(4, 2) - fourth row, second column - you do not want to perform action on Cell(5, 2) is there isn't a Row 5! To answer your question:The Selection object. What else would return:Selection.cells(1).rowIndex
or
Selection.Information(wdEndOfRangeRowNumber) I understand all of the above. I understand how to determine if the selection is in a cell, how to determine row and col positions, check on # rows, cols, etc. That is not the question. The question is how do I determine the specific table that the cell/cells is/are in? Assume that the selection is within a single cell of a table. How do I know what table? How would I set a variable (of type table) to the table containing the cell and selection?

fumei
04-07-2006, 09:37 PM
Well then you should have asked that question. Sheesh. We can't read minds. You asked with respect to what object the row and col. The selection object is the answer.
How would I set a variable (of type table) to the table containing the cell and selection?Answer:Dim oTable As Word.Table
Set oTable = Selection.Tables(1)will create a Table object of the table the selection is currently in.

Please note, again, that this is NOT, repeat, NOT the same thing as what you mention in your previous sentence/question, which was:
The question is how do I determine the specific table that the cell/cells is/are in?Two questions - how do I set a object for the table? Easy = Set oTable = Selection.Tables(1); how do I determine the specific table...this is not so easy, unless of course by "specific" you mean THIS table..in which case you are back to the easy answer.