K
ker_01
This is a followup to my thread from the end of last week;
I'm working with a document format that someone else created, and I need to
extract the data from various table (and nested table) cells in a reliable
fashion.
I'm now able to determine what cell of a table I'm in, and at what level of
"nesting". There is one piece still eluding me. In this template, I have at
least one situation where the parent table has multiple nested tables *in the
same cell*. So in the parent table's cell (1,1) I have a 2x2 child table,
then some text in the parent table cell, then another 2x2 child table still
in that same parent cell.
If I am looping through every cell in every table, and then every cell in
every sub-table (in whatever order; I can re-arrange the data later), how do
I differentiate between child table 1 and child table 2 in the same parent
table cell?
My desired end product will be something like the following, but since I
don't know the Word object model, I'm backing into it:
For each table in document.tables
For each cell in table.cells
'For each subtable in table.cell ?
'For each cell in subtable ?
MyVariable = cell.range.text
'do my processing
'Next
'Next
Next
Next
I appreciate any suggestions!
Thank you,
Keith
Current code, which is designed just to verify the current location/table:
Sub LocationMacro()
Dim iSelectionRowEnd As Integer
Dim iSelectionRowStart As Integer
Dim iSelectionColumnEnd As Integer
Dim iSelectionColumnStart As Integer
Dim lngStart As Long
Dim lngEnd As Long
Dim lngNestLvl As Long
Dim pTableNumber As String
' 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
lngNestLvl = Selection.Cells.NestingLevel
' 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 ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count & _
Chr(13) & Chr(13) & lngNestLvl & Chr(13) & Chr(13) & _
"The selection covers " & Selection.Cells.Count & " cells, from
Cell(" & _
iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _
iSelectionRowEnd & "," & iSelectionColumnEnd & ")."
End If
End Sub
I'm working with a document format that someone else created, and I need to
extract the data from various table (and nested table) cells in a reliable
fashion.
I'm now able to determine what cell of a table I'm in, and at what level of
"nesting". There is one piece still eluding me. In this template, I have at
least one situation where the parent table has multiple nested tables *in the
same cell*. So in the parent table's cell (1,1) I have a 2x2 child table,
then some text in the parent table cell, then another 2x2 child table still
in that same parent cell.
If I am looping through every cell in every table, and then every cell in
every sub-table (in whatever order; I can re-arrange the data later), how do
I differentiate between child table 1 and child table 2 in the same parent
table cell?
My desired end product will be something like the following, but since I
don't know the Word object model, I'm backing into it:
For each table in document.tables
For each cell in table.cells
'For each subtable in table.cell ?
'For each cell in subtable ?
MyVariable = cell.range.text
'do my processing
'Next
'Next
Next
Next
I appreciate any suggestions!
Thank you,
Keith
Current code, which is designed just to verify the current location/table:
Sub LocationMacro()
Dim iSelectionRowEnd As Integer
Dim iSelectionRowStart As Integer
Dim iSelectionColumnEnd As Integer
Dim iSelectionColumnStart As Integer
Dim lngStart As Long
Dim lngEnd As Long
Dim lngNestLvl As Long
Dim pTableNumber As String
' 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
lngNestLvl = Selection.Cells.NestingLevel
' 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 ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count & _
Chr(13) & Chr(13) & lngNestLvl & Chr(13) & Chr(13) & _
"The selection covers " & Selection.Cells.Count & " cells, from
Cell(" & _
iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _
iSelectionRowEnd & "," & iSelectionColumnEnd & ")."
End If
End Sub