Identify table cell with insertion point in VBA?

E

Ed

Is there a way using VBA to identify the column and row of the Word table
cell containing the insertion point, or that has been selected? (An
equivalent to Excel's ActiveCell)

Ed
 
J

Jean-Guy Marcil

Ed was telling us:
Ed nous racontait que :
Is there a way using VBA to identify the column and row of the Word
table cell containing the insertion point, or that has been selected?
(An equivalent to Excel's ActiveCell)

Try the following. Note that if you are dealing with tables containing
merged cells, then it will not work.

'_______________________________________
Dim IndexCol As Long
Dim IndexRow As Long


With Selection
If Not .Information(wdWithInTable) Then
MsgBox "Selection must be in a table cell.", _
vbCritical, "Invalid Selection"
Exit Sub
Else
IndexCol = .Columns(1).Index
IndexRow = .Rows(1).Index
MsgBox "The coordinates of the top left cell of " _
& "the current selection is row " & IndexRow _
& " and column " & IndexCol & ".", _
vbInformation, "Active Cell"
End If
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Ed,

Try something like:

Sub IDCell()
Dim oCol As Single
Dim oRow As Single

oCol = Selection.Information(wdStartOfRangeColumnNumber)
oRow = Selection.Information(wdStartOfRangeRowNumber)
MsgBox ("The IP is located in column " & oCol & ", row " & oRow & ".")



End Sub
 
E

Ed

Thank you, Greg!
Greg Maxey said:
Ed,

Try something like:

Sub IDCell()
Dim oCol As Single
Dim oRow As Single

oCol = Selection.Information(wdStartOfRangeColumnNumber)
oRow = Selection.Information(wdStartOfRangeRowNumber)
MsgBox ("The IP is located in column " & oCol & ", row " & oRow & ".")



End Sub
 
J

Jean-Guy Marcil

Ed was telling us:
Ed nous racontait que :
Thank you! I never expected to get 3 different ways to do the same
thing!

In fact, 2 ways... Greg and Helmut's ways are the same, it is just that Greg
wrapped it up in a more complete sub.

My fault, I forgot about
Selection.Information(wdEndOfRangeRowNumber)
which is not bothered by merged cells (But you still have to consider that
in your code, unless you know for a fact that the table cannot contain
merged cells).

Also, remember that if many cells are selected,
Selection.Information(wdEndOfRangeRowNumber)
gives you the bottom right cell index, my clumsier method gives you the top
left cell.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top