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:

  1. Install WordWriter.dll and collections.dll in the Global Assembly Cache:

    1. Copy WordWriter.dll to C:\WINNT\Assembly.


    2. Copy collections.dll to C:\WINNT\Assembly.

  2. Register WordWriterCOM.dll:

    1. Open a command prompt window and move to the directory WordWriter\doc-samples\bin. For example, enter cd WordWriter\doc-samples\bin.


    2. Enter regsvr32 WordWriterCOM.dll. A dialog box will inform you that WordWriterCOM.dll registered successfully; click Ok.


    3. 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.

ASP/Basic/StringVar/StringVar.asp

[View Source]
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:

  1. In Microsoft Word, create a new file.


  2. From the Insert menu, select Field... to open the Field dialog.

  3. From the Field names list, select MergeField.

  4. In the Field name text box, enter a name for the merge field. For example, enter ProductName.

  5. Click Ok. The merge field will be displayed in the document in the format <<Field Name>>.


  6. 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:

  1. Create an instance of the WordTemplate object:
    	Dim oWW
    	...
    	Set oWW = Server.CreateObject("SoftArtisans.WordTemplate")
  2. 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.

  3. 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.

  4. Call WordTemplate.Process to enter values from the data source into the template's merge fields:

    	oWW.Process
  5. 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.