views:

370

answers:

4

How can I write to/generate a *.doc file programmatically using c or c++? Is there a (open source/cross platform) library to do this? If this is not possible, can write an *.odt file and then convert it to *.doc? Thanks in advance!

EDIT: Anders Abel commented that *.rtf file type is an option, so any suggestions on this one are also accepted.

+1  A: 

The .doc format is, as you probably have noticed not open. An easier workaround is often to write an .rtf file, which opens nicely in MS Word. The .rtf format is text based and publicly documented. As long as keeping to quite simple content, writing .rtf is simple.

Anders Abel
*.doc is now an open type of document. But you did not give an examples or suggestions about *.rtf.
Levo
+1  A: 

You can use Word COM.

Here's one example of somebody doing it in C++ but it looks a little outdated and I'm not sure how complete it is. I usually use C# where it's really easy to simply add a reference to the Microsoft.Office.Word Interop.

McAden
+3  A: 

Joel has an interesing article about this topic: http://www.joelonsoftware.com/items/2008/02/19.html

Basically he suggest either:

  1. Use MS Word via COM to create the document.
  2. Generate another format that MS Word will load, such as RTF. RTF has the advantage that it is a text format. So you can generate a template document with place holders, and then just run a substitution operation when you want to generate your documents.
Douglas Leeder
Thanks, I'll take option 2.
Levo
+2  A: 
  1. Either use Microsoft Word Interop with object interface. Word needs to be installed.
  2. Use rtf for simple stuff. For complicated stuff rtf usually fails or gets troublesome.
  3. Consider docx. Microsoft published toolkit to worki with docx.
  4. Doc is open, but the specification is written for aliens. You can try to chew through it.
Kugel