Free Support Forum - aspose.com

How to set data values in MS word file through Aspose.Word API

We are developing a .NET application that needs to write data into an MS Word template / document file - hopefully we can use Aspose.Word

We want to know if we can do the following:

1. How do we declare fields in MS Word document / template as placeholders (for example as Form Fields, mail merge fields, etc.) for the data that our application will be inserting into the Word template?

2. At runtime, our application needs to set the values of these fields through the Aspose.Word api and load the resulting document in an instance Word for further editing by the user.

3. Do the same thing but load data into tables that we have “predefined” in MS Word. The numbers of rows in the table will be determined at runtime. We would like to “format” the table directly in our word template (eg borders, fonts, colors, etc.)

Thanks in advance for your assistance.

Hi,

Thank you for your interest in our products.

1. Basically, there are several ways to populate existing document using Aspose.Word. The most popular way is mail merging. There are two ways to use mail merge: with regions or without. Performing mail merge without regions is simple and very similar to how mail merge works in Word, i.e. the document should contain Word MERGEFIELD and optionally NEXT fields. Concerning mail merge with regions, you should enclose the merge fields into two special fields marking the repeatable region so the portions inside the document will dynamically grow. Please refer here for more information:

http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.MailMerge.html

One of other ways is placing bookmarks into the document, navigating through them and inserting appropriate objects. The same way you can use form fields, move to them and set their values. Please see the DocumentBuilder methods, this is the key class for building the documents dynamically.

2. As described above, you can easily populate the fields with appropriate values, save the resulting document and run a Word instance if needed.

3. You can build tables and set their formatting at runtime using DocumentBuilder again. Then you can place necessary fields inside them, save the template and then populate it - and this is only one of possible approaches.

You can vary and combine different manners of Aspose.Word API use since its object model is pretty easy to learn. Anyway, I’m sure that Aspose.Word fits your requirements completely and would help you to accomplish your tasks.

Dmitry,

Thanks for getting back to me. I think we’ll give it a try and will post here when we have more questions.

Kind regards,

Dave Dunlap