We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Trying to copy a table, populate original then create dups of original table with data

Hi there, I have an existing word doc, I want to copy a table (I have done that with a Var called ClonedTable)

Then i want to populate data in the original table (adding rows as I go). Then at a change of key value I want to paste ClonedTable after existing table then populate it, rinse and repeat.

For the life of me I cannot work out the code to do this.

I have tried…

Dim oTable As Aspose.Words.Tables.Table

oTable = doc.Range.Bookmarks("VSTable").BookmarkStart.GetAncestor(NodeType.Table)

Dim ClonedTable As Aspose.Words.Node

ClonedTable = oTable.Clone(True)

Dim par As Paragraph = New Paragraph(doc)

oTable = ClonedTable.GetAncestor(NodeType.Table)
oTable.Rows(0).Cells(0).FirstParagraph.AppendChild(New Run(doc, "222"))

oTable.ParentNode.InsertAfter(par, oTable)
par.ParentNode.InsertAfter(ClonedTable, par)

But it just gives " Object reference not set to an instance of an object"

Any help would be most welcome

Thanks Matt

@matthewsimpson,
To get the desired result please remove the following line:

oTable = ClonedTable.GetAncestor(NodeType.Table)

The code will update the original table (FirstParagraph.AppendChild method) and then insert the cloned table after the original one.

Please let us know, if I understood the issue correctly. Also, could you please ZIP and attach an example input and output document, which would demonstrate us the desired behavior? We will then provide you more information according to your requirement.

@matthewsimpson I have create s simple VB.NET code example that demonstrates how you can achieve what you need:

Dim doc As New Document("C:\Temp\in.docx")
Dim builder As New DocumentBuilder(doc)

Dim oTable As Table = doc.Range.Bookmarks("VSTable").BookmarkStart.GetAncestor(NodeType.Table)
Dim clonedTable As Table = oTable.Clone(True)

' Fill the table with dummy data
' Place the clonned table after and fill it with data again.
For tableIndex As Integer = 0 To 5

    ' Add new table
    If (tableIndex > 0) Then
        Dim delimiterPara As New Paragraph(doc)
        oTable.ParentNode.InsertAfter(delimiterPara, oTable)
        oTable = clonedTable.Clone(True)
        delimiterPara.ParentNode.InsertAfter(oTable, delimiterPara)
    End If

    Dim emptyRow = oTable.LastRow.Clone(True)
    For dummyRowIndex As Integer = 0 To 5

        ' Add new row
        If (dummyRowIndex > 0) Then
            oTable.AppendChild(emptyRow.Clone(True))
        End If

        ' Fill row with data
        For cellIndex As Integer = 0 To oTable.LastRow.Cells.Count - 1
            builder.MoveTo(oTable.LastRow.Cells(cellIndex).FirstParagraph)
            builder.Write(String.Format("This is {0} table, {1} row, {2} cell", tableIndex, dummyRowIndex, cellIndex))
        Next
    Next

Next

doc.Save("C:\Temp\out.docx") 

Also, please, see the attached input and output documents. in.docx (12.8 KB)
out.docx (11.0 KB)

You sir are a star ! :star: I will try this tomorrow and let you know.

Thanks very much

Hi Alexey,

On the above line i am getting the error of ;

"Value of type ‘Node’ cannot be converted to ‘Table’

I also have the DIM statement as

Dim oTable As Aspose.Words.Tables.Table

Is that too deep into the tree ?

Any ideas ?

Many thanks

Matt

Alexey,

Don’t worry, the code was getting confused with a .NET table and a ASPOSE Table, so defining the table as a;

Dim oTable As Aspose.Words.Tables.Table

Solved the problem.

Many thanks for your help !

Matt

@matthewsimpson It is perfect that code works now. Please feel free to ask in case of any issues, we will be glad to help you.