Microsoft KB Archive/270906

= How to use ASP to generate a Rich Text Format (RTF) document to stream to Microsoft Word =

Article ID: 270906

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q270906



SUMMARY
This article describes how you can generate Rich Text Format (RTF) files with ASP script and then stream those files to Microsoft Word. This technique provides an alternative to server-side Automation of Microsoft Word for run-time document generation.

Note This article requires that you have a copy of the Northwind.mdb sample database installed from Microsoft Access 2000, from Microsoft Access 2002, or from Microsoft Office Access 2003.



MORE INFORMATION
There are drawbacks to generating Word documents with ASP server-side Automation:
 * Performance Microsoft Word is an out-of-process Automation server; out-of-process calls take longer to execute.


 * Scalability As an Automation server, Microsoft Word has substantial overhead because the Winword.exe file must be loaded in order for you to reap the benefits of Word's rich object model.

By generating your documents from scratch and without the use of the Word object model, you can make your ASP application more scaleable and also increase performance. This approach is used in the following samples that demonstrate how to use the FileSystemObject to create an RTF file that can be interpreted in Microsoft Word at browser clients.

The first sample shows how to use ASP to create an RTF document that contains headers and footers, contains a table, and uses different font styles and colors for text throughout the document. The first sample uses ADO to access data from the sample Northwind Access database to create a 90-plus-page document.

The second sample demonstrates how to create an RTF document similar to that of a Word mail merge. The resulting document contains headers and footers, various paragraph formatting and page breaks, and uses different font styles and types through the document. This sample also uses ADO to access data from the Northwind database to create a 170-plus-page document.

Note The Rich Text Format (RTF) specification is a public specification to generate RTF-compatible text files. You can use the documentation for the specification at the following Microsoft Developer Network (MSDN) Web site as a resource to help you build your own RTF files. However, the specification is provided &quot;as-is&quot;, and is not supportted by Microsoft Technical Support. For the latest version of the RTF specification, see the following MSDN Web site:

Rich Text Format (RTF) Specification 1.6

http://msdn2.microsoft.com/en-us/library/aa140277(office.10).aspx

ASP Sample 1
The following ASP demonstrates creation of a 90-plus-page RTF document that contains a report of calculated figures.

Note The variable sConn in this code sample contains the path to the Northwind database. Confirm that the path supplied is appropriate for your installation of Office. You also need to supply Write access to the folder containing the ASP so that the document can be created. This access should be limited to specific individuals under a Windows NT domain, but can be applied to Everyone for anonymous access. <%@ Language=VBScript %> <%

Dim sRTF, sFileName, sConn sConn = &quot;c:\program files\microsoft office\office\samples\northwind.mdb&quot;

Response.Buffer = True

'Create the file for the RTF Dim fso, MyFile Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;) sFileName = &quot;sample1.doc&quot; Set MyFile = fso.CreateTextFile(Server.MapPath(&quot;.&quot;) & &quot;\&quot; & _                                  sFileName, True)

MyFile.WriteLine(&quot;{\rtf1&quot;)

'Write the color table (for use in background and foreground colors) sRTF = &quot;{\colortbl;\red0\green0\blue0;\red0\green0\blue255;&quot; & _ &quot;\red0\green255\blue255;\red0\green255\blue0;&quot; & _ &quot;\red255\green0\blue255;\red255\green0\blue0;&quot; & _ &quot;\red255\green255\blue0;\red255\green255\blue255;}&quot; MyFile.WriteLine(sRTF)

'Write the title and author for the document properties MyFile.WriteLine(&quot;{\info{\title Sample RTF Document}&quot; & _                   &quot;{\author Microsoft Developer Support}}&quot;)

'Write the page header and footer MyFile.WriteLine(&quot;{\header\pard\qc{\fs50 &quot; & _                   &quot;ASP-Generated RTF\par}{\fs18\chdate\par}\par\par}&quot;) MyFile.WriteLine(&quot;{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100&quot; & _                   &quot;\fs18 Page &quot; & _                    &quot;{\field{\*\fldinst PAGE}{\fldrslt 1}} of &quot; & _                    &quot;{\field{\*\fldinst NUMPAGES}{\fldrslt 1}} \par}&quot;)

'Write a sentence in the first paragraph of the document MyFile.WriteLine(&quot;\par\fs24\cf2 This is a sample \b RTF \b0 &quot; & _                   &quot;document created with ASP.\cf0&quot;)

'Connect to the database in read-only mode Dim conn, rs  Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;) conn.Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot; conn.Mode = 1   'adModeRead=1 conn.Open sConn 'Execute a query that returns ID, Product name and amount of sale Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;) rs.Open &quot;SELECT [Order Details].OrderID AS ID, &quot; & _ &quot;Products.ProductName AS Name, &quot; & _ &quot;[Order Details].[UnitPrice]*[Quantity] AS Amount &quot; & _ &quot;FROM Products INNER JOIN [Order Details] ON &quot; & _ &quot;Products.ProductID = [Order Details].ProductID &quot; & _ &quot;ORDER BY [Order Details].OrderID&quot;, conn, 3  'adOpenStatic = 3

MyFile.WriteLine(&quot;{&quot;) 'Start table MyFile.WriteLine(&quot;\par\par\fs24&quot;)

'Write the table header row (appears at top of table on each page) sRTF = &quot;\trowd\trhdr\trgaph30\trleft0\trrh262&quot; & _ &quot;\cellx2000\cellx7000\cellx9000&quot; & _ &quot;\pard\intbl\qc\b\i\ul Order ID \ul0\i0\b0\par\cell&quot; & _ &quot;\pard\intbl\ql\b\i\ul Product \ul0\i0\b0\par\cell&quot; & _ &quot;\pard\intbl\qr\ul\b\i Amount \ul0\i0\b0\par\cell&quot; & _ &quot;\pard\intbl\row&quot; MyFile.WriteLine(sRTF)

dim LastID dim CurID dim CurTotal dim bFirstRow

bFirstRow=true

do while not (rs.eof)

if LastID<>rs(&quot;ID&quot;).Value and not(bFirstRow) Then

'Starting on a row for a different id, so add the last total and 'then a blank row sRTF = &quot;\trowd\trgaph30\trleft0\trrh262&quot; & _ &quot;\cellx2000\cellx7000\clbrdrb\brdrdb\cellx9000&quot; & _ &quot;\pard\intbl\cell\pard\intbl\cell\pard\intbl\qr\b &quot; & _ FormatCurrency(CurTotal, 0, False, False, True) & _ &quot;\b0\cell\pard\intbl\row&quot; & _ &quot;\trowd\trgaph30\trleft0\trrh262&quot; & _ &quot;\cellx2000\cellx7000\cellx9000&quot; & _ &quot;\pard\intbl\cell\pard\intbl\cell\pard\intbl\cell&quot; & _ &quot;\pard\intbl\row&quot; MyFile.WriteLine(sRTF)

CurID = rs(&quot;ID&quot;).Value CurTotal = 0 elseif bFirstRow then CurID = rs(&quot;ID&quot;).Value else CurID = &quot;&quot; end if

'Add a new row with the ID, the Product name and the amount 'Note: Amounts over 1000 are formatted with red. sRTF = &quot;\trowd\trgaph30\trleft0\trrh262&quot; & _ &quot;\cellx2000\cellx7000\cellx9000&quot; & _ &quot;\pard\intbl\qc &quot; & CurID & &quot;\cell&quot; & _ &quot;\pard\intbl\ql &quot; & rs(&quot;Name&quot;).Value & &quot;\cell&quot; If rs(&quot;Amount&quot;).Value >1000 Then sRTF = sRTF & &quot;\pard\intbl\qr\cf6 &quot; & _ FormatNumber(rs(&quot;Amount&quot;).Value, 0, False, False, True) & _ &quot;\cf0\cell\pard\intbl\row&quot; else sRTF = sRTF & &quot;\pard\intbl\qr &quot; & _ FormatNumber(rs(&quot;Amount&quot;).Value, 0, False, False, True) & _ &quot;\cell\pard\intbl\row&quot; end if

MyFile.WriteLine(sRTF)

LastID = rs(&quot;ID&quot;).Value CurTotal = CurTotal + rs(&quot;Amount&quot;).Value rs.MoveNext bFirstRow=false

loop

MyFile.WriteLine(&quot;}&quot;) 'End Table

'Add a page break and then a new paragraph MyFile.WriteLine(&quot;\par \page&quot;) MyFile.WriteLine(&quot;\pard\fs18\cf2\qc &quot; & _                   &quot;This sample provided by Microsoft Developer Support.&quot;)

'Close the recordset and database rs.Close conn.Close Set rs = Nothing Set conn = Nothing 'close the RTF string and file MyFile.WriteLine(&quot;}&quot;) MyFile.Close

Response.Write _ &quot;&quot; %>

ASP Sample 2
The following ASP demonstrates a simulation of a Word mail merge or form letter. Each page in the 170-plus-page document is generated from a record in an ADO recordset.

Note The variable sConn in this code sample contains the path to the Northwind database. Confirm that the path supplied is appropriate for your installation of Office. You also need to supply Write access to the folder containing the ASP so the document can be created. This access should be limited to specific individuals under an NT domain, but can be applied to Everyone for anonymous access. <%@ Language=VBScript %> <%  Dim sRTF, sConn sConn = &quot;c:\program files\microsoft office\office\samples\northwind.mdb&quot;

Response.Buffer = True

'Create the file for the RTF Dim fso, MyFile, sFileName Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;) sFileName = &quot;sample2.doc&quot; Set MyFile = fso.CreateTextFile(Server.MapPath(&quot;.&quot;) & &quot;\&quot; & _                                  sFileName, True)

MyFile.WriteLine(&quot;{\rtf1&quot;)

'Write the font table sRTF = &quot;{\fonttbl {\f0\froman\fcharset0 Times New Roman;}&quot; & _ &quot;{\f1\fswiss\fcharset0 Arial;}&quot; & _ &quot;{\f2\fmodern\fcharset0 Courier New;}}&quot; MyFile.WriteLine sRTF

'Write the title and author for the document properties MyFile.WriteLine(&quot;{\info{\title Sample RTF Document}&quot; & _                   &quot;{\author Microsoft Developer Support}}&quot;)

'Write the document header and footer MyFile.WriteLine(&quot;{\header\pard\qc\brdrb\brdrs\brdrw10\brsp100&quot; & _                   &quot;{\fs50 Northwind Traders} \par}&quot;) MyFile.WriteLine(&quot;{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100&quot; & _                   &quot;{\fs18 Questions?\par Call (111)-222-3333, &quot; & _                    &quot; Monday-Friday between 8:00 am and 5:00 pm} \par}&quot;)

'Connect to the database in read-only mode Dim conn, rs  Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;) conn.Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot; conn.Mode = 1   'adModeRead=1 conn.Open sConn

'Execute a query that returns ID, Product name and amount of sale Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;) rs.Open &quot;SELECT Orders.ShippedDate, Orders.ShipVia, &quot; & _ &quot;Orders.OrderDate, Orders.OrderID, &quot; & _ &quot;Customers.ContactName FROM Customers INNER JOIN Orders ON &quot; & _ &quot;Customers.CustomerID = &quot; & _ &quot;Orders.CustomerID WHERE (((Orders.ShippedDate) Between &quot; & _ &quot;#1/1/1998# And #3/31/98#))&quot;,conn,3 'adOpenStatic = 3

Do While Not (rs.eof) 'Write the &quot;body&quot; of the form letter MyFile.WriteLine  &quot;\fs26\f1&quot;   'Default font

MyFile.WriteLine  &quot;\pard&quot; MyFile.WriteLine  &quot;\par\par\par\par&quot; MyFile.WriteLine  &quot;\par RE: Order #&quot; & rs.Fields(&quot;OrderID&quot;).Value MyFile.WriteLine  &quot;\par&quot; MyFile.WriteLine  &quot;\par&quot; MyFile.WriteLine  &quot;\par &quot; & rs.Fields(&quot;ContactName&quot;).Value & &quot;, &quot; MyFile.WriteLine  &quot;\par&quot; MyFile.WriteLine  &quot;\par Thank you for your order on &quot; & _ rs.Fields(&quot;ShippedDate&quot;).Value & _ &quot;. Your order has been shipped: &quot; MyFile.WriteLine  &quot;\par&quot; MyFile.WriteLine  &quot;\par&quot;

MyFile.WriteLine  &quot;\pard \li720 {\f2&quot; MyFile.WriteLine  &quot;\par \b Order Number \b0 \tab &quot; & _ rs.Fields(&quot;OrderID&quot;).Value MyFile.WriteLine  &quot;\par \b Shipped By \b0 \tab &quot; & _ rs.Fields(&quot;ShipVia&quot;).Value MyFile.WriteLine  &quot;\par \b Shipped On \b0 \tab &quot; & _ rs.Fields(&quot;ShippedDate&quot;).Value MyFile.WriteLine  &quot;\par}&quot; MyFile.WriteLine  &quot;\pard&quot; MyFile.WriteLine  &quot;\par&quot; MyFile.WriteLine  &quot;\par Northwind Traders is committed to &quot; & _ &quot;bringing you products of the &quot; & _ &quot;highest quality from all over the world. If &quot; & _ &quot;at any time you are not completely satisfied &quot; & _ &quot;with any of our products, you may &quot; & _ &quot;return them to us for a full refund.&quot; MyFile.WriteLine  &quot;\par&quot;

MyFile.WriteLine  &quot;\pard {\fs18 \qc \i&quot; MyFile.WriteLine  &quot;\par Thank you for choosing Northwind Traders!&quot; MyFile.WriteLine  &quot;\par}&quot;

rs.MoveNext

'Add a page break and then a new paragraph If Not(rs.eof) Then MyFile.WriteLine(&quot;\pard \page&quot;)

Loop

'Close the recordset and database rs.Close conn.Close Set rs = Nothing Set conn = Nothing 'close the RTF string and file MyFile.WriteLine(&quot;}&quot;) MyFile.Close

Response.Write _ &quot;&quot;

%>

