Joining tables in Word

Hello,

I have a problem where I need to join several separate tables in a word document. The desired outcome is to have the appearance of a single large table in the end.

I can get the merge to populate the tables as required, however I am having blank lines between each of the tables which I cannot seen to work around. I have attached a word document of my desired output, current template configuration and the current output.

Any advice to point me in the right direction will be much appreciated. I do my development in vb.

Joe Di Giulio

Hi
Thanks for your request. You can get the desired output by setting RemoveEmptyParagraphs option before executing mail merge:
https://reference.aspose.com/words/net/aspose.words.mailmerging/mailmergecleanupoptions/
But you should also use the latest version of Aspose.Words (we improved the behavior of this option in the recently released version). You can download the latest version form here:
https://releases.aspose.com/words/net
Best regards,

Hello Alexey,
I downloaded the latest version of Aspose Words and the results are now a lot better.
However I still seem to have a hopefully minor problem when I want to skip a table because it has no data. I am getting a blank line in place of the next table in the document taking that spot. I have attached a sample of what is happening and my template. Hopefully it is a problem with my template!
Joe

Hi Joe,
Thanks for your inquiry.
I managed to reproduce the issue on my side regarding the empty paragraphs left over even after RemoveEmptyRegions is true. Your request has been linked to the approriate issue, you will informed as soon as this is fixed.
In the mean time I think you can use the code from this article to manually remove these paragraphs during mail merge. Please see the code below. The CreateDataSourceFromDocumentRegions method can be found in the linked article.

// Execute mail merge with the data.
doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.ExecuteWithRegions(data);
// Manually handle how empty regions are removed.
doc.MailMerge.FieldMergingCallback = new HandleRemoveRegionParagraphs();
doc.MailMerge.ExecuteWithRegions(CreateDataSourceFromDocumentRegions(doc, null));
doc.Save("Document Out.docx");
public class HandleRemoveRegionParagraphs: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        Node parentTable = args.Field.Start.GetAncestor(NodeType.Table);
        // Merging would of removed these empty paragraphs, just remove the table now as well.
        if (parentTable.ParentNode != null)
            parentTable.Remove();
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do Nothing.
    }
}

If you have any further queries, please feel free to ask.
Thanks,

Hi,

I have tried your suggestion and have had limited success. I had to convert the code to VB so possibly I have made an error.

I have attached the new resulting document - the highlighted sections should not have appeared.

Here is the code I am using:

doc.MailMerge.RemoveEmptyParagraphs = True
doc.MailMerge.RemoveEmptyRegions = False

doc.MailMerge.Execute(dsResultSet.Tables(0))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(1))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(2))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(3))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(4))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(5))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(6))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(7))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(8))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(9))
doc.MailMerge.ExecuteWithRegions(dsResultSet.Tables(10))


’ Manually handle how empty regions are removed.
doc.MailMerge.FieldMergingCallback = New HandleRemoveRegionParagraphs()
doc.MailMerge.ExecuteWithRegions(CreateDataSourceFromDocumentRegions(doc, Nothing))
Private Shared Function CreateDataSourceFromDocumentRegions(ByVal doc As Document, ByVal regionsList As ArrayList) As DataSet
    Const tableStartMarker As String = "TableStart:"
    Dim dataSet As New DataSet()
    Dim tableName As String = Nothing

    For Each fieldName As String In doc.MailMerge.GetFieldNames()
        If fieldName.Contains(tableStartMarker) Then
            tableName = fieldName.Substring(tableStartMarker.Length)

        ElseIf tableName IsNot Nothing Then
            ' Only add the table name as a new DataTable if it doesn’t already exists in the DataSet.
            If dataSet.Tables(tableName) Is Nothing Then
                Dim table As New DataTable(tableName)
                table.Columns.Add(fieldName)


                ' We only need to add the first field for the handler to be called for the fields in the region.
                If regionsList Is Nothing OrElse regionsList.Contains(tableName) Then
                    table.Rows.Add("FirstField")

                End If

                dataSet.Tables.Add(table)
            End If
            tableName = Nothing
        End If
    Next fieldName

    Return dataSet
End Function

Public Class HandleRemoveRegionParagraphs
    Implements IFieldMergingCallback
    Public Sub FieldMerging(ByVal args As FieldMergingArgs)
        Dim parentTable As Node = args.Field.Start.GetAncestor(NodeType.Table)


        ' Merging would of removed these empty paragraphs, just remove the table now as well.
        If parentTable.ParentNode IsNot Nothing Then
            parentTable.Remove()
        End If
    End Sub

    Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

        ' Do nothing.
    End Sub
    Private Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

        ' Do nothing.
    End Sub
End Class

Thanks,

Joe

Hi Joe,
Thank you for additional information. Maybe you should just set RemoveEmptyRegions option to true. As I can see in your code, you set it to false.
Also, it would be great if you export your DataSet to XML and attach it here so we can test with the same data as you do.
Best regards,

Hello Alexey,

I have tried changing the setting of RemoveEmptyRegions but still have no success.

Please find attached a copy of the dataset and full template as requested. The dataset is not complete as this is ‘work in progress’.

Kind regards,

Joe

Hi Joe,
Thanks for this additional information.
You implementation of IFieldMergingCallback had a few problems, please see the code below. Yellow highlighting are what you should add to your current code to make it work and red is the code you should remove.

Public Class HandleRemoveRegionParagraphs
    Implements IFieldMergingCallback
    Public Sub FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging
        Dim parentTable As Node = args.Field.Start.GetAncestor(NodeType.Table)

        ' Merging would of removed these empty paragraphs, just remove the table now as well.
        If parentTable IsNot Nothing Then
            If parentTable.ParentNode IsNot Nothing Then
                parentTable.Remove()
            End If
        End If
    End Sub
    Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

        ' Do nothing.
    End Sub
    Public Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

        ' Do nothing.
    End Sub
End Class

If you have any further queries, please feel free to ask.
Thanks,

Hello Adam,

I made the changes as you advised and it looks a lot better!

Thank you

Joe

The issues you have found earlier (filed as WORDSNET-5104) have been fixed in this .NET update and in this Java update.