|
Quick Start
> Create a Document with WordTemplate
Create a Document with WordTemplate
OfficeWriter's WordTemplate object opens a WordWriter
template file, populates it with data from one or more data sources, and generates
a new Word file. A WordWriter template is a file created in Microsoft
Word that contains merge fields. A merge field displays a data source field name
(for example, a database column name) where a data source value will be inserted.
Merge fields are bound to a data source in OfficeWriter code. When you run the code,
OfficeWriter replaces the merge fields with values from the data source and creates a
new Word file.
Code Sample: Using a Variable as a Data Source

| OfficeWriter includes SAWW3CCW.dll, a COM callable wrapper (CCW) that allows you to use
the .NET WordTemplate object from ASP. For instructions, see
Using OfficeWriter in ASP. |
Step 1: Create a Template

To add a merge field to a template, open the Insert
menu and select Field. The merge field display format is <<Field Name>>.
To generate a Word file with OfficeWriter, first create a template:
- In Microsoft Word, create a new file.
- From the Insert menu, select Field... to open
the Field dialog.
- From the Field names list, select MergeField.
- In the Field name text box, enter a name for the merge
field. For example, enter ProductName.
- Click Ok. The merge field will be displayed in the document
in the format <<Field Name>>.
- To insert additional merge fields, repeat steps 2-4.
- Save the template to a folder that OfficeWriter can access at runtime.
The ASPNET account or the authenticated user account must have Read access
to the template file.
The Main Document and Repeat Blocks
A WordWriter template may include merge fields
in repeat blocks, merge fields in the main document, or both.
The "main document" is any part of the template that is not within a repeat
block. Merge fields within the main document are bound to a data source
by the method SetDataSource.
Merge fields within repeat blocks are bound to a data source
by the method SetRepeatBlock. |
Step 2: Write the OfficeWriter Code

Next, create an ASP.NET script that uses OfficeWriter to open the template, fill in
merge field values, and generate a new Word file. The VB.NET and C# samples
above generate a file from the template StringVarTemplate.doc.
This template contains a single merge field - <<Product Name>>.
The data source for the merge field is a single-element array.
The WordTemplate object represents the template Word file.
The WordTemplate object is in the SoftArtisans.OfficeWriter.WordWriter namespace.
The object can be referenced as
SoftArtisans.OfficeWriter.WordWriter.WordTemplate. To minimize typing and errors, use
an Import directive to import the namespace to the aspx page, and reference
the object as WordTemplate, without the namespace prefix:
<%@ Import Namespace="SoftArtisans.OfficeWriter.WordWriter" %>
To generate a new Word file with OfficeWriter:
- Create an instance of the
WordTemplate object:
// [C#]
WordTemplate wt = new WordTemplate();
' [VB.NET]
Dim wt As New WordTemplate()
- Call
WordTemplate.Open to open a template Word file:
// [C#]
wt.Open(Server.MapPath("./StringVarTemplate.doc"));
' [VB.NET]
wt.Open(Server.MapPath("./StringVarTemplate.doc"))
The Open method takes the template's path (as in the example),
a System.IO.Stream object, a WordApplication object, or a
Document object.
OfficeWriter supports Microsoft Word 97, 2000, 2002 (XP), and 2003.
Do not use Open to open files created in earlier versions of Microsoft Word. |
- Call
WordTemplate.SetDataSource to bind the template's
merge fields to a data source:
// [C#]
object[] arrValue = {"SoftArtisans OfficeWriter"};
string[] arrName = {"ProductName"};
wt.SetDataSource(arrValue, arrName);
' [VB.NET]
Dim arrValue As Object() = {"SoftArtisans OfficeWriter"}
Dim arrName As String() = {"ProductName"}
wt.SetDataSource(arrValue, arrName)
This sample application uses a single string variable as a data source.
To set the data source to a single value, put the value in a single-element
object array, and the merge field name in a single-element string array. Pass the two
arrays to WordTemplate.SetDataSource.
Call WordTemplate.Process to enter values from the data source into the
template's merge fields:
// [C#]
wt.Process();
' [VB.NET]
wt.Process()
Call WordTemplate.Save to save the generated file or
stream it to the browser.
In the samples above the file is streamed to the browser:
// [C#]
wt.Save(Page.Response, "StringVarOutput.doc", false);
' [VB.NET]
wt.Save(Page.Response, "StringVarOutput.doc", False)
When you pass Save an HttpResponse object (Page.Response)
OfficeWriter will stream the file to the browser.
The method's second parameter specifies a
default name for the generated file; this name will be displayed in the browser's
File Download dialog. The third parameter specifies whether the file should open in
the browser window or in Microsoft Word; if the parameter is set to False, the file
will open in Word.
OfficeWriter
also allows you to save the generated file on the server. For more information,
see Output Options.

Copyright 2005 © SoftArtisans, Inc. All Rights Reserved.
|