Forums     Knowledge Base     OfficeWriter Online     
 
This documentation is for
OfficeWriter v3.0.5
.NET Platform

View Docs for Another
Version or Platform

WordTemplate in Depth > Using WordWriter in ASP

Using WordWriter in ASP

WordWriter includes a COM callable wrapper (CCW) that allows you to use the .NET WordTemplate object from ASP. To use WordWriter in ASP:

  1. All of the system requirements for running WordWriter in ASP.NET must be satisfied.

  2. SAWW3NET.dll must be installed in the Global Assembly Cache. For instructions, see ASP Installation.

Example: Generating a Word File in ASP

The following ASP example generates a file from a template that contains a single merge field. The data source is a single-element array.

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.

Next, create an ASP script that uses WordWriter to open the template, fill in merge field values, and generate a new Word file. The following VBScript example opens a template that contains a single merge field - <<ProductName>>. The data source for the merge field will be a single-element array.

To generate a new Word file with WordWriter in ASP:

  1. Create an instance of the WordTemplate object:
    	Dim wwt
    	...
    	Set wwt = Server.CreateObject("SoftArtisans.WordTemplate")
  2. Call WordTemplate.Open to open a template Word file:
    	wwt.Open("C:\Templates\Template.doc")

    The Open method takes the complete 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:
    	Dim arrValue(0)
    	Dim arrName(0)
    	...
    	arrValue(0) = "SoftArtisans WordWriter"
    	arrName(0) = "ProductName"
    	...
    	wwt.SetDataSource arrValue, arrName
    

    To set a 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:

    	wwt.Process
  5. Call WordTemplate.Save to save the generated file or stream it to the browser. The following streams the file to the browser:

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



Copyright 2005 © SoftArtisans, Inc. All Rights Reserved.