Fog Creek Software
Discussion Board

Word API?

We have some applications that use Word automation to create documents. This does the job, but is awkward to program and requires too much testing to assure that it works with all versions of Word.  We would also like to have the option of creating the documents on a web server.

We use ExcelWriter to make .xls files.  It is a COM dll that is easy to install and use and fast.  Does anyone know of a similar library for .doc/.rtf?   


Scott McKissock
Wednesday, February 12, 2003

And it needs to make tables.

Scott McKissock
Wednesday, February 12, 2003

I've looked at this; there are components out there which do the job, but they're all pretty expensive.

One workaround is that Word will open just about anything, as long as you rename it .doc - HTML, for example, which is dead easy to generate on the server. Ditto RTF.

Neil E
Wednesday, February 12, 2003

We tried the HTML option at first, but it didn't give us enough control. Some of the docs we make are included in government publications, so they have to look exactly right. 

Cost isn't a problem, up to about $2,500.

At one point I took a look at the rtf spec, thinking it would be best to write the .rtf generator myself, but I couldn't tell if it would take a week or a month (or more) to do it.

Scott McKissock
Wednesday, February 12, 2003

Actually RTF is fairly simple, The one I wrote for output from our report generator took less than a week to write.
I did cheat and used the RTF output from Crystal reports to start with as it's a lot easier than the output from either WordPad or Word to decode.
RTF is basically a table of fonts at the top followed by
a markup language using { } rather than < >

Name the RTF as a .DOC and noone is any the wiser

Peter Ibbotson
Wednesday, February 12, 2003

I agree with Peter, RTF isn't that difficult to do by hand.

If you don't want to reverse engineer RTF, goto and do a lookup on RTF - they have several Microsoft documents on their 'standard'.

I do usually start like Peter does, make my document in word/wordpad/crystal and then save it to RTF. This gives a good starting point for development.

Wednesday, February 12, 2003

There are plenty of PDF generators, and some are free. That would be good for printable forms, at least.

Troy King
Wednesday, February 12, 2003

Thanks for the posts. It is looking like it would make sense to do this myself.  But is there a good library out there? It seems pretty basic. I don't want to be swamped by issues like this page is portrait, this page is landscape, etc.

Thanks again.

Scott McKissock
Wednesday, February 12, 2003

Think of it as like asking for a HTML library.
For output purpose what would you like that library to do for you? About the only function thats useful for output is "htmlize" a string.
The RTF equivalent I had reads (in VB):

Private Sub RTFOutText(FNo As Integer, S As String)
    Dim L As Long
    Dim S2 As String
    For L = 1 To Len(S)
        S2 = Mid$(S, L, 1)
        If (InStr("\{}", S2) <> 0) Then Print #FNo, "\";
        Print #FNo, S2;
End Sub

Thats about it, (I really should rewrite this for speed but no-ones complained yet) Parsing RTF for display purposes isn't actually too hard either. Below is the rtf from word pad for "hello world":

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit;}\viewkind4\uc1\pard\f0\fs20 Hello World\par

Stripping this down a bit to a slightly more sensible file gives:
    {\f0\fswiss\fcharset0 Arial;}
\pard\f0\fs20 Hello World\par\

Now my code dumps fonts out as
{\f0\fnil\fcharset0\fprq2 Ariel;}
so I don't think you even need to know the font family name. I have NO idea what the \fnil\fcharset\fprq2 bit is there for now but it works great for me.

The font sizes (\fsXX) are in half points. It's worth pointing out that Word and WordPad DO NOT look the same on screen but should print out the same on paper. Word does a much better job of kerning letters to match the printed output.

MSDN has the RTF spec and a sample reader written in C IIRC.

Peter Ibbotson
Thursday, February 13, 2003

Thanks Peter.  I have what I need to get started.

Scott McKissock
Friday, February 14, 2003


Tuesday, March 4, 2003

*  Recent Topics

*  Fog Creek Home