Clone Word Document and then Execute MailMerge (Regions)

I am getting the same problem Here is my code

          If Not xmlTemplateToConvertInfo.DocumentData Is Nothing AndAlso xmlTemplateToConvertInfo.DocumentData.Tables.Count > 0 Then
                    For Each Table As DataTable In xmlTemplateToConvertInfo.DocumentData.Tables
                        Dim docTemp As Document

                        If Table.Rows.Count > 0 Then
                            If Not (Table.Rows(0).RowState.ToString() = DataRowState.Deleted.ToString()) Then
                                docTemp = doc.Clone(True)
                                docTemp.MailMerge.Execute(Table)

                            End If
                        End If
                    Next

                    ' doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields
                    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields Or MailMergeCleanupOptions.RemoveContainingFields Or MailMergeCleanupOptions.RemoveEmptyParagraphs Or MailMergeCleanupOptions.RemoveUnusedRegions
                    'this final pass handles the repeating table scenario
                    doc.MailMerge.ExecuteWithRegions(xmlTemplateToConvertInfo.DocumentData)

                    doc.MailMerge.UseNonMergeFields = True
                    doc.MailMerge.DeleteFields()
                    doc.UpdateFields()
                    doc.UnlinkFields()

I don’t know how to clone the document and then execute.Please let me know

@Gurpreet.kaur,

Unfortunately, your question is not clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your usecase. This will help us to understand your scenarios, and we will be in a better position to address your concerns accordingly.

Also, to ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 19.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create expected document by using MS Word.
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue/scenario and provide you more information. Thanks for your cooperation.

Hi Awais,

From my previous Code you can see my rows are returning multiple time due to that it document comes multiple times .do you have any solution so that document shows up one time only

  Try
            If File.Exists(xmlTemplateToConvertInfo.TemplateFileNamePath) Then
                doc = New Document(xmlTemplateToConvertInfo.TemplateFileNamePath)
                If doc.MailMerge.GetFieldNames().Length = 0 Then Throw New Exception("Old 2003 document Xml" + xmlTemplateToConvertInfo.OutputFileName)
                'This handles the embedded images as a callout to the MailMergeInsertImage class (defined below)
                doc.MailMerge.FieldMergingCallback = New MailMergeInsertImage(xmlTemplateToConvertInfo.DocumentData)
                ' doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions
                'First pass passes each table to the mail merge one at a time. This is so that
                'we don't have to setup table regions in the document. As long as all table fields are uniquely 
                'named, this approach will work. This is exactly how the legacy CEGES document generator worked
                'which is what the current datasets were designed to target.
                If Not xmlTemplateToConvertInfo.DocumentData Is Nothing AndAlso xmlTemplateToConvertInfo.DocumentData.Tables.Count > 0 Then
                    For Each Table As DataTable In xmlTemplateToConvertInfo.DocumentData.Tables
                        '  Dim docTemp As Document

                        If Table.Rows.Count > 0 Then
                            If Not (Table.Rows(0).RowState.ToString() = DataRowState.Deleted.ToString()) Then
                                '  docTemp = doc.Clone(True)
                                ' docTemp.MailMerge.Execute(Table)

                            End If
                        End If
                    Next

                    ' doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields
                    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields Or MailMergeCleanupOptions.RemoveContainingFields Or MailMergeCleanupOptions.RemoveEmptyParagraphs Or MailMergeCleanupOptions.RemoveUnusedRegions
                    'this final pass handles the repeating table scenario
                    doc.MailMerge.ExecuteWithRegions(xmlTemplateToConvertInfo.DocumentData)

                    doc.MailMerge.UseNonMergeFields = True
                    doc.MailMerge.DeleteFields()
                    doc.UpdateFields()
                    doc.UnlinkFields()
                    'Saves the document to a stream using the specified format
                    generatedDocumentFileName = SaveMergeTemplate(doc, xmlTemplateToConvertInfo)
                End If

            End If
        Catch ex As Exception
            Throw New Exception("DocumentConverterBO.GenerateDocument Exception" + ex.Message + xmlTemplateToConvertInfo.TemplateFileNamePath, ex.InnerException)
        Finally
            doc = Nothing
        End Try

@Gurpreet.kaur,

Unfortunately, it is difficult to reproduce the exact same problem/scenario on our end by just using the code you provided. We need these resources from your end to be able to completely understand and reproduce the problem with latest version of Aspose.Words for .NET 19.11 on our end and then suggest you a solution.