Log in

View Full Version : [SOLVED:] select table inside table



joe19p
06-22-2019, 09:44 AM
I'm new to the forum, I need to write a code in Word read data in 2 tables (one of them inside another table).

I can read data from the first table, but from the table that is inside a combined cell I can not. attached image, differentiating the tables in colors. orange table father and red table son (inside a combined cell)


Asignación #: 4234817


ASUNTO:
6150166
Fecha Apertura:
Jun 21 2019 5:28PM
Tiempo Desplazamiento:
29 minutos
Tiempo de Reparación:
98 minutos
Ingeniero:
ALEXANDER RONNY CAHUANA MULLO


ONLINE:
No
Fecha de Cierre:
Jun 21 2019 7:10PM
Espera de sistema:
4 minutos
Transacciones:
0
Contacto:
CONTACTO 1










ATENCION


ID
Módulo
Problema Inicial
Tiempo Reparación
Codigo Cierre
Codigo Problema
Codigo Reparación
Causal
Comentarios Reparación


4668635
BOVEDA
FALLA DE GAVETA
98
Cierre Exitoso
Boveda
AJUSTE
MALA OPERACIÓN
TEXTO(*)






24470

gmaxey
06-22-2019, 10:43 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table, oNestedTable As Table
Dim oCell As Cell
Set oTbl = ActiveDocument.Tables(1)
For Each oCell In oTbl.Range.Cells
If oCell.Tables.Count > 0 Then
Set oNestedTable = oCell.Tables(1)
'This should give you your table of interest.
Exit For
End If
Next oCell
lbl_Exit:
Exit Sub
End Sub

joe19p
06-22-2019, 11:08 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table, oNestedTable As Table
Dim oCell As Cell
Set oTbl = ActiveDocument.Tables(1)
For Each oCell In oTbl.Range.Cells
If oCell.Tables.Count > 0 Then
Set oNestedTable = oCell.Tables(1)
'This should give you your table of interest.
Exit For
End If
Next oCell
lbl_Exit:
Exit Sub
End Sub


Hi gmaxey! Thank you for the reply. It worked!

gmaxey
06-22-2019, 11:22 AM
You're welcome. BTW, if you document is always formatted such that the nested table is in cell index 22, then you could avoid the loop and just use:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table, oNestedTable As Table
Dim oCell As Cell
Set oTbl = ActiveDocument.Tables(1)
Set oCell = oTbl.Range.Cells(22)
If oCell.Tables.Count > 0 Then
Set oNestedTable = oCell.Tables(1)
End If
lbl_Exit:
Exit Sub
End Sub

PeterH_NZ
06-22-2019, 06:19 PM
G'Day Joe19p

This is a tad more simple:

Public Sub NestedTables()
Dim nestedTable As Word.Table
Dim theTable As Word.Table


' Iterate through all tables in the current document
For Each theTable In ActiveDocument.Tables


' We are only interested in Tables that contain a nested Table
If theTable.Tables.Count > 0 Then


' This code assumes that there is only one nested table in your parent Table
Set nestedTable = theTable.Tables(1)
Exit For
End If
Next


' Check that we actually caught a Table containing a nested Table
If Not nestedTable Is Nothing Then


' Do Something with your nested Table here
Debug.Print "My nested table contains " & nestedTable.Rows.Count & " rows"
End If
End Sub ' NestedTables


Good luck - Peter