Inserting Rows into Middle of Table


#1

Please can you provide an example in VB of how to insert rows in the middle of a table, so that they retain the formatting from the row above. I have been able to insert rows, but am struggling to get the rows at the right place in the table, and with the correct formatting. Thanks


#2

Here is a sample code:

Dim filename As String = Application.StartupPath + "\doc1.doc"

Dim doc As Document = New Document(filename)

' ordinal of table that you want to work with

Dim tablenum As Integer = 0

' ordinal of row that you want to copy and insert

Dim rownum As Integer = 0

Dim table As Table = doc.GetChild(NodeType.Table, tablenum, True)

If table Is Nothing Then Return

Dim row As Row = table.Rows(rownum).Clone(True)

table.Rows.Insert(rownum, row)

doc.Save(Application.StartupPath + "\doc1_modified.doc")

Don't hesitate to ask if you have further questions .


#3

Wow - that must be the fastest response ever! Thanks. I will try it


#4

Hi,

I have run into a couple of problems with this. Firstly, Aspose.Words seems to think there is an additional row in the table, which I cannot detect using Word VBA code. Running a table.row.GetText returns "a", but this row does not exist in Word. Secondly, how do I add text into the cells in the new row?

Thanks again for your help


#5

Please be more descriptive on your first question. It is unclear what the problem is.

Concerning your second question - on how to add text into the cells in the new row. The most convenient way of doing it is using the DocumentBuilder.MoveToCell + DocumentBuilder.Write methods.


#6

Thanks. I will investigate this. I’ve also figured out that the first problem is not a problem after all (afraid it was some dodgy code I had entered whilst trying to figure out how to clone rows. Sorry about that!). Thanks again for your help