B4J Question [Solved] OpenDocx get Text from Cells in MSWord Table

GabrielM

Member
Licensed User
Longtime User
I have a MS Word (docx) file with a table in it, 2 rows each with 3 cells.
What I am trying to figure out is how to log the text of each cell.

The table looks like this:


The code (below) found on examples on this forum in this form is logging just the first cell in the first row. I can get the value for the table number of rows but I fail to understand how to iterate.
Actually, I am not sure how to pass the parameter of the row needed to the method: getRow(row_number).
Any hint, please.

B4X:
Dim doc As JavaObject = OpenDocx(File.DirAssets, "1.docx")
    Dim tables As List = doc.RunMethod("getTables", Null)

    For Each table As JavaObject In tables
        '
        Dim rowNumber As Int = table.RunMethod("getNumberOfRows", Null)
        Log("rowNumber= " & rowNumber)    ' get the number of rows
        '
        Dim row As JavaObject = table.RunMethod("getRow", Array(0))
        Dim cell As JavaObject = row.RunMethod("getCell", Array(0))
        Dim paragraphs As List = cell.RunMethod("getParagraphs", Null)
        For Each p As JavaObject In paragraphs
            Dim runs As List = p.RunMethod("getRuns", Null)
            If runs.IsInitialized Then
                For Each r As JavaObject In runs
                    Dim text As String = r.RunMethod("getText", Array(0))
                    If text <> Null Then
                        Log(text)
                    End If
                Next
            End If
        Next
    Next
 

Attachments

  • jpoi_msword.zip
    12.6 KB · Views: 318

GabrielM

Member
Licensed User
Longtime User
Ok, guess I get it right this time:


B4X:
    Dim doc As JavaObject = OpenDocx(File.DirAssets, "1.docx")
    Dim tables As List = doc.RunMethod("getTables", Null)
        

    For Each table As JavaObject In tables
        Dim rows As List = table.RunMethod("getRows", Null)
        
        For Each row As JavaObject In rows
            Dim cells As List = row.RunMethod("getTableCells", Null)
            For Each cell As JavaObject In cells
                Dim paragraphs As List = cell.RunMethod("getParagraphs", Null)
                For Each p As JavaObject In paragraphs
                    Dim runs As List = p.RunMethod("getRuns", Null)
                    If runs.IsInitialized Then
                        For Each r As JavaObject In runs
                            Dim text As String = r.RunMethod("getText", Array(0))
                            If text <> Null Then
                                Log(text)
                            End If
                        Next
                    End If
                Next
            Next
        Next   
    Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…