Free Support Forum - aspose.com

ExecuteWithRegions - Table Rows Already There

I am trying to determine the best method for using ExecuteWithRegions. I would like to ExecuteWithRegions with a DataTable into a Word document table. If I have blank rows already there in the table in Word, and I have my region on the first row, Aspose just inserts new rows and “grows” the table in Word. What would be a good method to fill in the rows if they are already there using ExecuteWithRegions? Should I do the ExecuteWithRegions command, and then remove the blank rows in code? I don’t want to do this with DocumentBuilder, because I don’t want to have to figure out what column to use as I iterate through the rows.

Hi

Thanks for your request. I have written the code for you. This code will remove empty rows after current row during mail merge. I have used the following template.

«TableStart:mytable»«col1»

«col2»

«col3» «TableEnd:mytable»

Sub Main()

Dim table As DataTable = New DataTable("mytable")

table.Columns.Add("col1")

table.Columns.Add("col2")

table.Columns.Add("col3")

Dim row1 As DataRow = table.NewRow()

row1(0) = "test"

row1(1) = "test"

row1(2) = "test"

table.Rows.Add(row1)

Dim row2 As DataRow = table.NewRow()

row2(0) = "test"

row2(1) = "test"

row2(2) = "test"

table.Rows.Add(row2)

Dim doc As Document = New Document("in.doc")

' add a hadler to MergeField event

AddHandler doc.MailMerge.MergeField, AddressOf MergeDocumentHandler

doc.MailMerge.ExecuteWithRegions(table)

doc.Save("out1.doc")

End Sub

Private Sub MergeDocumentHandler(ByVal sender As Object, ByVal e As Reporting.MergeFieldEventArgs)

Dim builder As DocumentBuilder = New DocumentBuilder(e.Document)

'move to mergw field

builder.MoveToField(e.Field, True)

If builder.CurrentParagraph.ParentNode.NodeType = NodeType.Cell Then

'get current row

Dim row As Row = CType(builder.CurrentParagraph.ParentNode, Cell).ParentRow

'get index of next row

Dim index As Integer = row.ParentTable.Rows.IndexOf(row) + 1

' if there is after current row some row the remove this row

While index < row.ParentTable.Rows.Count

row.ParentTable.Rows(index).Remove()

End While

End If

End Sub

I hope that this code will help you.

Best regards.