Using WordWriter in ASP |
 |
WordWriter includes WordWriterCOM.dll, a COM callable wrapper (CCW) that allows to
use the .NET WordTemplate object from ASP. The CCW
is a COM object that makes internal calls to the WordWriter .NET assembly.
Therefore, to use WordWriter in ASP, all of the
system requirements
for running WordWriter in ASP.NET must be satisfied.
The object model available in ASP includes all the methods available in ASP.NET, except
Open(templateStream)
and Save(oStreamObj).
The ASP Programmer's Reference
explains how to use each of the WordTemplate methods in ASP.
To install WordWriter for ASP:
- Install WordWriter.dll and collections.dll in the Global Assembly Cache:
- Copy WordWriter.dll to C:\WINNT\Assembly.
- Copy collections.dll to C:\WINNT\Assembly.
- Register WordWriterCOM.dll:
- Open a command prompt window and move to the directory WordWriter\doc-samples\bin.
For example, enter cd WordWriter\doc-samples\bin.
- Enter regsvr32 WordWriterCOM.dll. A dialog box will
inform you that WordWriterCOM.dll registered successfully; click Ok.
- Enter iisreset to reset IIS.
Example: Generating a Word File in ASP
The following ASP sample generates a file from a template
that contains a single merge field. The data source is a
single-element array.
|
|
| WordWriter includes several ASP sample applications. You can run the samples and
view the ASP code from the
Index of ASP Samples. |
|
|
To generate a Word file with WordWriter, 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.
|
 |
|
To add a merge field to a template, select open the Insert
menu and select Field. The merge field display format is <<Field Name>>.
|
Next, create an ASP script that uses WordWriter to open the template, fill in
merge field values, and generate a new Word file. The sample above
generates a file from the template StringVarTemplate.doc
(included in WordWriter\doc-samples\samples\ASP\basic\stringvar).
This template contains a single merge field - <<Product Name>>.
The data source for the merge field is a single-element array.
To generate a new Word file with WordWriter in ASP:
- Create an instance of the
WordTemplate object:
Dim oWW
...
Set oWW = Server.CreateObject("SoftArtisans.WordTemplate")
- Call
WordTemplate.Open to open a template Word file,
for example:
oWW.Open Server.MapPath("./StringVarTemplate.doc")
The Open method takes thecomplete path and file name of the
template Word file. Note that in ASP you cannot pass a stream to Open. |
WordWriter supports Microsoft Word 97, 2000, and 2002 (XP).
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, for example:
Dim arrValue(0)
Dim arrName(0)
...
arrValue(0) = "SoftArtisans WordWriter"
arrName(0) = "ProductName"
...
oWW.SetDataSource arrValue, arrName
StringVar.asp uses a single variable as a data source.
To set the data source to a single value, put the value in a single-element
array, and the merge field name in a single-element array. Pass the two
arrays to WordTemplate.SetDataSource.
Call WordTemplate.Process to enter values from the data source into the
template's merge fields:
oWW.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:
oWW.Save Response, "StringVarOutput.doc", False
When you pass the ASP Response object to Save,
WordWriter will stream the file to the client.
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.
WordWriter also allows you to save the generated file on the server. For more information,
see Output Options.
Copyright © 2003, SoftArtisans, Inc.