Search code examples
vb.netasposeaspose.words

Aspose.Word - Adding rows to an existing table in VB.Net


In a Word template document, I have a table header defined and would like to add data (Multiple rows) to the same table programmatically using aspose and having hard time in doing this.

I found few posts on online for doing this but all of them are written JAVA and functions that are used in these posts are not available in VB.Net.

https://www.aspose.com/community/forums/thread/648997/reg-adding-rows-dynamically-to-the-existing-table-in-the-document.aspx

getLastRow() function doesn't exists in Table Class.(from above post).

Can some one point me to right documentation or provide a solution for my problem. Missing Width Thanks in Advance!


Solution

  • Please use LastRow method to get last row of table in VB using Aspose.Words for .NET 17.3. Please check complete code as following.

    I am Tilal Ahmad, developer evangelist at Aspose.

    Dim doc As New Document("input.docx")
    ' Retrieve the first table in the document.
    Dim table As Table = DirectCast(doc.GetChild(NodeType.Table, 0, True), Table)
    table.FirstRow.RowFormat.HeadingFormat = True
    For i As Integer = 1 To 15
    ' Clone the last row in the table.
    Dim clonedRow As Row = DirectCast(table.LastRow.Clone(True), Row)
    clonedRow.RowFormat.HeadingFormat = False
    ' Remove all content from the cloned row's cells. This makes the row ready for
    ' new content to be inserted into.
    For Each cell As Cell In clonedRow.Cells
        cell.FirstParagraph.Runs.Clear() 
        cell.CellFormat.ClearFormatting()
        cell.FirstParagraph.AppendChild(New Run(doc, "hello text"))
    Next
    
    ' Add the row to the end of the table.
    
    table.AppendChild(clonedRow)
    Next
    
    doc.Save("Table.AddCloneRowToTable Out.doc")