Consulting

Results 1 to 5 of 5

Thread: select table inside table

  1. #1

    Get data from table inside table

    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(*)

    Table inside Table.jpg
    Last edited by joe19p; 06-22-2019 at 11:11 AM.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3

    Thumbs up

    Quote Originally Posted by gmaxey View Post
    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!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Jun 2019
    Location
    Wellington
    Posts
    8
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •