Mail Merge from relational database

We have an ASP.NET/SQL Server web application that needs to have a massive “mail merge” functionality. We have implemented the free Open XML SDK solution for our users; however, the users have already built 100+ Microsoft Word templates and we are running into issues because each template seems to be different. What does Aspose.Words provide that would solve our issue? Do you have an example of bringing one-to-many database tables into MS Word? Thanks.

I would like to correct my above statement. It really isn’t a mail merge. The application needs to produce one document with a lot of DOCVariables in it. Does anyone have any examples of the best way to do this using Aspose.Words? Thx

Hi Marvin,

Thanks
for your inquiry.

First of all please note that Aspose.Words for .NET
is a class library that enables your applications to perform a great
range of document processing tasks. Aspose.Words supports DOC, DOCX,
RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats. With
Aspose.Words you can generate, modify, convert, render and print
documents without utilizing Microsoft Word®. For more information,
please go through the documentation below:
http://www.aspose.com/docs/display/wordsnet/Introducing+Aspose.Words+for+.NET

Secondly, please share following detail for investigation purposes.

  • Please attach your input Word documents.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us, we will then provide you more information about your query along with code.

I have attached my input template Word document and my target Word document showing the desited behavior. The paragraphs highlighted are those that have a DocVariable inside of it to be replaced by data from our SQL Server.


There can be more than one Detail that needs to be repeated in the document. And inside that Detail, there can be more than one record that needs to be repeated in the document.

Thanks for you immediate response!

Let me know if you have any other questions.

Hi Marvin,

Thanks
for sharing the documents.

A field in a Word document is a complex structure
consisting of multiple nodes that include field start, field code, field
separator, field result and field end. Fields can be nested, contain
rich content and span multiple paragraphs or sections in a document. The
Field class is a “facade” object that provides properties and methods
that allow to work with a field as a single object.

The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively.

The
content between the field start and separator is the field code. The
content between the field separator and field end is the field result.
The field code typically consists of one or more Run objects that
specify instructions. The processing application is expected to execute
the field code to calculate the field result.

In you case, I suggest you please use the following code snippet to replace the DocVariable with your contents. Hope this helps you. Please let us know if you have any more queries.


var doc = new Aspose.Words.Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection collection = doc.GetChildNodes(NodeType.FieldStart, true);

foreach (FieldStart start in collection)

{

if (start.FieldType == FieldType.FieldDocVariable)

{

builder.MoveTo(start);

builder.Writeln("Your contents...");

}

}

foreach (FieldStart start in collection)

{

if (start.FieldType == FieldType.FieldDocVariable)

{

start.GetField().Remove();

}

}

doc.Save(MyDir + "Out.docx");