Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Excerpt

The following example generates a Word file from a template that contains the merge fields ProductName, CompanyName, and URL. The WordWriter code gets the values for these fields from an array of objects.

To set a template's data source to an array, first create an object array of field values and a string array of field names:

Csharp

//--- Create an object array of field values
object[] arrValue = {"SoftArtisans", "WordWriter", "http://www.softartisans.com"};

//--- Create a string array of field names
string[] arrName = {"CompanyName", "ProductName", "URL"};
Vbnet
Wiki Markup
Code Block
languagevb
'--- Create an object array of field values.
Dim arrValue As Object() = {"SoftArtisans", "WordWriter", "http://www.softartisans.com"}

'--- Create a string array of field names.
Dim arrName As String() = {"CompanyName", "ProductName", "URL"}

Each merge field in the template will bind to a field name/field value pair. When creating field name and field value arrays to use as a data source, remember:

...

First, create the WordTemplate object and open a template file:

Code Block
c#
c#
titleC#

	string templatePath;
	WordTemplate wt = new WordTemplate();
	wt.Open(templatePath);
Code Block
vb.net
vb.net
titleVB.NET

	Dim templatePath As String
	Dim wt As New WordTemplate()
	wt.Open(templatePath)

You can call the SetDataSourcemethod before or after opening the template (that is, before or after calling Open). When you call SetDataSource, pass the two arrays you created to the method as parameters:

Code Block
c#
c#
titleC#

	wt.SetDataSource(arrValue, arrName);
Code Block
vb.net
vb.net
titleVB.NET

	wt.SetDataSource(arrValue, arrName)

The Process method enters the array values in the template's merge fields:

Code Block
c#
c#
titleC#

	wt.Process();
Code Block
vb.net
vb.net
titleVB.NET

	wt.Process()

The Save either saves the new Word file on the server, or streams it to the browser. In this example, WordWriter streams the generated file to the client:

Code Block
c#
c#
titleC#

	wt.Save(Page.Response, "ArrayVarsOutput.doc", false);
Code Block
vb.net
vb.net
titleVB.NET

	wt.Save(Page.Response, "ArrayVarsOutput.doc", False)

...