tags:

views:

487

answers:

4

I am writing a program using Qt. I want it to output to .doc to preserve formatting but all that is supported by Qt is plain text, ODF and HTML formats. ODF and HTML will preserve the formatting but then I would have to copy and paste this to a .doc file. I want to be able to save a .doc file and not have to worry about doing this. I have Googled this many times but have found no solution. Any help would be greatly appreciated.

Thanks

A: 

When you say ".doc" format I will assume you mean the "Word 97-2003 Document" format used by Microsoft Word. Microsoft Office can open .html files just fine, so you could go that route. If Word is installed on the some computer that you're running the program on, you could copy the data to the clipboard and use automation to have Word paste it and save it in .doc format.

What formatting are you using specifically?

Bill
I was hoping to use the Word 97-2003 Document format as you stated. I did do some research and found out that the ODF format is XML based just like the new Word .docx format. However, I still have been unable to find a way to convert the .odt file to .docx. The application will be cross-platform so on the mac/linux machines the ODF file will be fine but for the Windows machine I needed a way for Word 2003 to open the file. Word 2007 actual opens the .odt file but I wanted it to work on both version of Word. Thanks
Aaron McKellar
A: 

As you stated, QTextDocumentWriter support only plain text, ODF and HTML formats.

If you need to read only your document on Windows, you can produce a PDF instead of trying to produce a .doc:

 QPrinter printer;
 QTextDocument document;

 //TODO: Put stuff to print in your 'document'

 printer.setOutputFormat(QPrinter::PdfFormat);
 printer.setOutputFileName("/foobar/nonwritable.pdf");

 document.print( printer );

Another possible solution, if you have absolutely to modify your document in Word, try an ODF add-on for Word 2003 to open your ODF file.

esavard
+2  A: 

Only way to create valid .doc file is to use Microsoft Office. It can be done from your program with OLE Automation (more on that problem here - http://www.joelonsoftware.com/items/2008/02/19.html).

Qt provides ActiveQt framework for working with COM objects. So you can load Word and convert .html to .doc using it's COM interface. Of course, MS Office must be installed.

Ivan
A: 

There is another alternative to this.. practically you can call it hacking but kinda simple way as i find it for myself.

1) Create HTML template on how you wan the document to look like.. 2) Then open the html document using MSWord Document 3) Adjust the template (coz somethings might be miss adjusted) 3) The places that you would like to write some data / text put somekinda marker such like #offaddress# or so ... 4) Save the document 5) Rename the document back to HTML.. now open the document .. DONT PANIC the document will written with lots of MSWord Document codes and stuff .. just dun bother .. 6) Copy the entire code and paste it in ur Qt 7) set output to ur Qt as .doc file.. 8) B4 generating all u need to do is match the keywords that u put in the template such like #offaddress# with real data and come out with .doc file .. :)..

9) Voila you hv ur .doc file to users it wont matter coz it seems MsWord :)