Finding which cell in a table

M

Mark

I am using WORD upwards, please can someone help me with some code which
sets as an integers the table number, row number and cellnumber is the
activecell?

Mark
 
J

Jean-Guy Marcil

Mark was telling us:
Mark nous racontait que :
I am using WORD upwards, please can someone help me with some code
which sets as an integers the table number, row number and cellnumber
is the activecell?

Mark

Try this:

Dim CellCoordinate As String
Dim CurRange As Range


If Not Selection.Information(wdWithInTable) Then
MsgBox "You must select a cell in a table.", vbCritical, "Error"
Else
Set CurRange = Selection.Range
'In case users selects more than one cell
Selection.Collapse wdCollapseStart
CellCoordinate = CStr(ActiveDocument.Range(0,
CurRange.Start).Tables.Count)
CellCoordinate = CellCoordinate & CStr(Selection.Rows(1).Index)
CellCoordinate = CellCoordinate & CStr(Selection.Columns(1).Index)
Selection.Range.InsertAfter CellCoordinate
CurRange.Select
End If


But, it will not work if there are merged cells in the table.

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

Jay Taplin

Mark -

This should do it!

With Selection
MsgBox "Column: " &
CStr(.Information(wdStartOfRangeColumnNumber))
MsgBox "Row: " & .Information(wdStartOfRangeRowNumber)
End With

Jay Taplin MCP
 
J

Jean-Guy Marcil

Mark was telling us:
Mark nous racontait que :
Jean-Guy,

Thank you very much, you have been most helpful yet again!

Well, I could have been more helpful...
I should have thought of the information property, which works even with
merged cells. So using Jay's suggestion and my previous code, here is a
revised version that will work regardless of cell merging:

Dim CellCoordinate As String
Dim CurRange As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "You must select a cell in a table.", vbCritical, "Error"
Else
Set CurRange = Selection.Range
'In case users selects more than one cell
Selection.Collapse wdCollapseStart
CellCoordinate = CStr(ActiveDocument.Range(0,
CurRange.Start).Tables.Count)
With Selection
CellCoordinate = CellCoordinate _
& CStr(.Information(wdStartOfRangeColumnNumber))
CellCoordinate = CellCoordinate _
& CStr(.Information(wdStartOfRangeRowNumber))
End With
Selection.Range.InsertAfter CellCoordinate
CurRange.Select
End If



--
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