Free Support Forum -

Open Multiple Word Docs in a loop

To Aspose Development Team or any Aspose Expert:

We are trying to merge/print multiple records from a DataTable using a single Word document. The problem we are facing right now is that it prints only the last record in the DataTable.

Sub PrintMulipleRecords()

For Each Row In DataTable.Rows

Dim doc as Aspose.Word.Document = New Aspose.Word.Document("C:\WordDocument.doc")

' Get the license file


' GetDataSet



' MailMerge.Execute

' doc.Save("WordDocument.doc", Aspose.Word.SaveFormat.FormatDocument, Aspose.Word.SaveType.OpenInWord, Me.Response)


End Sub

The problem is that Aspose does not open/print the document until the End Sub is executed which results in only the last record in the datatable to be printed. How do we merge and print this word document for all records in the datatable?

Appreciate your prompt response and thanks in advance,

Tony John.

Normally, multiple documents cannot be send in one HTTP response. There are several workarounds existing but they are all pretty complex.

I can suggest that you:

1. Design your web server so that client should request each document separately.

2. Or combine these documents in one document.

3. Or zip them together and send an archive in response.

If it does not fit your application design than you can use a scripting approach. For this you need to save generated files with unique names in your web application folder first. Then send user a script in response with instructions to upload each of these files in a separate window. Here is an example that gives a basic idea:

Dim serverUrl As String = "http://" & Request.Url.Host + Request.ApplicationPath & "/"

Dim script As String = ""


' Generated script should look like:


Hope this helps,

Thanks a lot for your response Miklovan.

I see your point and would like to use the second approach and that is combine all documents into one. The reason is these documents are mailmerge to be printed immediately without needing the user to open each one of them. I would like to combine the merged word document from all records in my dataset into one big word document and using VBA in the Word document, would like to print in upon opened by the user.

So how do I merge multiple word documents into a single one and send it back to the client?

Your timely help and advice is greatly appreciated.

Tony John.

Here is a code that you can use to append one document to another:

Private Sub AppendDoc2(ByVal dstDoc As Document, ByVal srcDoc As Document)

For Each srcSection As Section In srcDoc

Dim dstSection As Node = dstDoc.ImportNode(srcSection, True, ImportFormatMode.KeepSourceFormatting)


Next srcSection

End Sub

You should create the emty document before the loop and then append all mailmerged document into it one by one. Please note that this combined document will have the empty section in the end that will look like empty page in MS Word. To remove it do


after the loop is finished.

Best regards,