0

I am looking for mail merge alternatives in my vb.net app. I have used the mail merge feature of word, and find that it is quite buggy when dealing with a large volume of documents. I am looking at alternate methods of generating the merge, and have come across open xml. I think this will probably be the answer I am looking for. I have come to understand that the merge will be entirely code-driven in vb.net. I have started playing around with the following code:

Dim wordprocessingDocument As WordprocessingDocument = wordprocessingDocument.Open("C:\Users\JasonB\Documents\test.docx", True)

    'for each simplefield (mergefield)
    For Each field In wordprocessingDocument.MainDocumentPart.Document.Body.Descendants(Of SimpleField)()
        'get the document instruction values
        Dim instruction As String() = field.Instruction.Value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries)


        'if mergefield
        If instruction(0).ToLower.Equals("mergefield") Then

            Dim fieldname As String = instruction(1)


           For Each fieldtext In field.Descendants(Of Text)()
                fieldtext.Text = "I AM TESTING"
            Next

        End If

        wordprocessingDocument.MainDocumentPart.Document.Save()
        wordprocessingDocument.Dispose()

Now this works great and all, but I am realizing that I need to create as many documents as I will have datarows (assuming I use a datatable to handle the data).

One suggestion I found was to loop through each datarow, take my document template, save it to a folder and insert the datarow data. This could mean however that I end up with 12,000 documents in a single folder that need to be joined later and converted to pdf.

Is there another option? The other thing that stood out to me is to create a new word document, and duplicate over the xml from the template, and then replace the values. I dont know however if there is a "simpler" way of doing this, thanks.

Jason Bayldon
  • 1,296
  • 6
  • 24
  • 41

1 Answers1

0

If you don't want to save all 12,000 documents to file you should be able to process, convert and email them one at a time using temporary files.

Converting the DOCX to PDF in .NET might be an issue but looks like it's possible using Word Automation (Saving Word DOCX files as PDF).

The bottom line is you don't need to generate all documents before emailing them if you perform the process one document at a time. You can use SmtpClient in VB.NET to email the PDF after it is generated.

In terms of creating the document I have seen reports generated where a simple string replace is used to replace a string such as '%FIRSTNAME%' with the person's name and so on. This isn't necessarily the best approach but can work quite well. This way you can create your template in Word or OpenOffice and then edit it in .NET using OpenXML.

Community
  • 1
  • 1
Lummo
  • 1,159
  • 9
  • 14
  • so lets say I have 12k records in a data table, and I want to merge all my data into one word document. I would still generate 12k temporary files? How would I go about writing them into one docx file that is 12k pages long so I can save it into pdf? – Jason Bayldon Aug 07 '13 at 12:57
  • I think in this case rather than generating 12,000 records you generate 1 document (per person), convert it to PDF, email it and then move on to the next one. This way you only deal with one person and one set of temporary files at a time. I don't think converting a 12,000 page document to a 12,000 page PDF is going to simplify the process. – Lummo Aug 09 '13 at 01:18
  • Does Open XML support a mail merge similar to word interop? Or will I be copying the template for each record and editing each of these files individually for each datarow? I noticed there is a mailmerge class, but it seems to connect a data source, not necessarily execute code to build more templates... I also do not think generating 1 document per person is really an option, at that point, I might as well just use interop to batch out 1000 letters at a time... – Jason Bayldon Aug 09 '13 at 02:30
  • There are a few blog posts that should help you out with the OpenXML mail merge. This one looks to be the most comprehensive: http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/11/01/7708.aspx – Lummo Aug 09 '13 at 08:49