none
Get content between two tables

    Question

  • Hi! I have a Word document, and in this document I can have several tables (they can be both word tables or excel tables pasted in the document).

    Anyway, sometimes there are huge tables that can be divided in multiple pages. The only way to know if the table is continuation of another one is to check if there's any strings between them (actually I have to get the value between them, cause sometimes there are strings like "Continuation of table xxx" and I have to check it)

    How can it be done?

    Thank you in advance!

    Thursday, October 21, 2010 7:42 PM

Answers

  • Here's a sample code to take the largest table in your doc (assuming largest is the higher value for rowcount x columncount)

    Is that what you need?

    Pls advise if not...

     

    Public
     Function
     GetBiggerTable() As
     Table
     
     Dim
     tItem As
     Table
     Dim
     tResult As
     Table
     
     For
     Each
     tItem In
     ActiveDocument.Tables
     
     If
     tResult Is
     Nothing
     Then
    
      
      Set
     tResult = tItem
      
     End
     If
    
     
     If
     (tItem.Rows.Count
     * tItem.Columns.Count
    ) > _
     (tResult.Rows.Count
     * tResult.Columns.Count
    ) Then
    
      
      Set
     tResult = tItem
      
     End
     If
    
     
     Next
     tItem
     
     Set
     GetBiggerTable = tResult
     
    End
     Function
    
    

     


    Thanks, but actually what I need is to get the very first Table in a word document, no matter its type (InlineShape, ShapeRange, etc). Because of this I can't just use ActiveDocument.Tables(0) or ShapeRange(0), for example.

     

    EDIT

     

    I have already found a solution, I just used Text attribute to convert the range to a string thus I can get the range length in characters. The largest one is (till the end of the document) is the one which its start position is before the others

    • Marked as answer by Bruce Song Thursday, October 28, 2010 1:21 AM
    Friday, October 22, 2010 6:37 PM

All replies

  • Hello Lucas,

    It's not clear (at least for me :) ) what you're trying to achieve.

    What's your goal? Count tables? Find the position of each table? Return the text between tables?

    ============

    Edit:

    To go through the tables in a document, you can use something like this... through the "item." you can see all properties a table object has. it may (or may not.. hehe) help you.

    Public Sub ListTables()
       
        Dim item As Table
       
        For Each item In ActiveDocument.Tables
           
            Debug.Print item.Rows.Count
           
        Next item
       
    End Sub

    Thursday, October 21, 2010 7:54 PM
  • Use:

    Dim i As Long
    Dim rngtable1 As Range
    Dim rngtable2 As Range
    Dim rnginbetween As Range
    With ActiveDocument
        For i = .Tables.Count To 2 Step -1
            Set rngtable2 = .Tables(i).Range
            Set rngtable1 = .Tables(i - 1).Range
            Set rnginbetween = .Range
            rnginbetween.Start = rngtable1.End
            rnginbetween.End = rngtable2.Start
            MsgBox rnginbetween.Text
        Next i
    End With


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Lucas Petrini" wrote in message news:1f5fb841-243c-488f-aeb2-9d82f742528f@communitybridge.codeplex.com...

    Hi! I have a Word document, and in this document I can have several tables (they can be both word tables or excel tables pasted in the document).

    Anyway, sometimes there are huge tables that can be divided in multiple pages. The only way to know if the table is continuation of another one is to check if there's any strings between them (actually I have to get the value between them, cause sometimes there are strings like "Continuation of table xxx" and I have to check it)

    How can it be done?

    Thank you in advance!


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Thursday, October 21, 2010 9:50 PM
  • Thanks a lot! I didn't think of that!

    BTW, there's only one last question: How can I get a range size? What I actually need is to get the first table in a range, but it can be in the ShapeRange collection, InlineShapes collection or in the Tables collection (actual Word tables), so I can't think of a way to compare the position of these different types of table. What I'm thinking about is to get the range (from table to the end of the document/range) of the first table in each collection and then take the one with the largest size, it would mean it is the first table in the range. But I don't know how to get the size or even if there's an easier way to achieve this.

    I hope you understand my issue.

    Thanks again!

    Friday, October 22, 2010 4:05 PM
  • Here's a sample code to take the largest table in your doc (assuming largest is the higher value for rowcount x columncount)

    Is that what you need?

    Pls advise if not...

    Public Function GetBiggerTable() As Table
      
      Dim tItem As Table
      Dim tResult As Table
      
      For Each tItem In ActiveDocument.Tables
        
        If tResult Is Nothing Then
          
          Set tResult = tItem
          
        End If
        
        If (tItem.Rows.Count * tItem.Columns.Count) > _
        (tResult.Rows.Count * tResult.Columns.Count) Then
          
          Set tResult = tItem
          
        End If
        
        Next tItem
      
      Set GetBiggerTable = tResult
      
    End Function
    

    Friday, October 22, 2010 6:01 PM
  • Here's a sample code to take the largest table in your doc (assuming largest is the higher value for rowcount x columncount)

    Is that what you need?

    Pls advise if not...

     

    Public
     Function
     GetBiggerTable() As
     Table
     
     Dim
     tItem As
     Table
     Dim
     tResult As
     Table
     
     For
     Each
     tItem In
     ActiveDocument.Tables
     
     If
     tResult Is
     Nothing
     Then
    
      
      Set
     tResult = tItem
      
     End
     If
    
     
     If
     (tItem.Rows.Count
     * tItem.Columns.Count
    ) > _
     (tResult.Rows.Count
     * tResult.Columns.Count
    ) Then
    
      
      Set
     tResult = tItem
      
     End
     If
    
     
     Next
     tItem
     
     Set
     GetBiggerTable = tResult
     
    End
     Function
    
    

     


    Thanks, but actually what I need is to get the very first Table in a word document, no matter its type (InlineShape, ShapeRange, etc). Because of this I can't just use ActiveDocument.Tables(0) or ShapeRange(0), for example.

     

    EDIT

     

    I have already found a solution, I just used Text attribute to convert the range to a string thus I can get the range length in characters. The largest one is (till the end of the document) is the one which its start position is before the others

    • Marked as answer by Bruce Song Thursday, October 28, 2010 1:21 AM
    Friday, October 22, 2010 6:37 PM