Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sun Mar 03, 2024 10:05 am

Hi!
I want to read a document with table(s) sequentially, i.e. paragraph by paragraph and when it is a table, detect it and read that table row by row.
I tried with this code

Dim oDok As Spire.Doc.Document
Dim oP As Spire.Doc.Documents.Paragraph

oDok = New Document()
oDok.LoadFromFile(sFil)

For Each oP In oDok.Sections(0).Paragraphs
sIn = oP.Text
Next

but when I come to a table in that document the table is not read but it jumps to the first paragraph after it.

Could anyone give me a hint how to iterate trough a document and get the text from every paragraph when it is a paragraph and from every cell when it is a table?

Thanks in advance

nagy53janos
 
Posts: 2
Joined: Sun Jan 26, 2020 3:23 pm

Mon Mar 04, 2024 6:07 am

Hello,

Thank you for your inquiry.
Please refer to the code below to implement reading the text content of paragraphs and tables in a Word document sequentially.
Code: Select all
Sub Main()
    ' Create a new Document object
    Dim document As Document = New Document

    ' Load a document file into the Document object
    document.LoadFromFile("")

    ' Loop through each Section in the document
    For Each section As Section In document.Sections
        ' Initialize a counter variable
        Dim i As Integer = 0

        ' Loop through the ChildObjects in the Section's Body
        Do While (i < section.Body.ChildObjects.Count)
            ' Get the current DocumentObject
            Dim documentObject As DocumentObject = section.Body.ChildObjects(i)

            ' Check if the DocumentObject is a Paragraph
            If documentObject.DocumentObjectType.Equals(DocumentObjectType.Paragraph) Then
                ' Cast the DocumentObject to a Paragraph
                Dim paragraph As Paragraph = CType(documentObject, Paragraph)
                ' Get the text content of the Paragraph
                Dim paraText As String = paragraph.Text
            ElseIf documentObject.DocumentObjectType.Equals(DocumentObjectType.Table) Then
                ' If the DocumentObject is a Table
                Dim table As Table = CType(documentObject, Table)
                ' Loop through each TableRow in the Table
                For Each tableRow As TableRow In table.Rows
                    ' Loop through each TableCell in the TableRow
                    For Each tableCell As TableCell In tableRow.Cells
                        ' Loop through each Paragraph in the TableCell
                        For Each paragraph As Paragraph In tableCell.Paragraphs
                            ' Get the text content of the Paragraph within the TableCell
                            Dim cellParagraphText As String = paragraph.Text
                        Next
                    Next
                Next
            Else
                ' Handle other types of DocumentObjects if needed
            End If

            ' Increment the counter
            i = (i + 1)
        Loop
    Next
End Sub

If you have any further questions or need additional guidance, please do not hesitate to reach out.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc