2

I am writing a program using Qt. I want it to output to .doc to preserve formatting, but all that is supported by Qt are 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 I haven't found a solution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron McKellar
  • 627
  • 4
  • 10
  • 20

4 Answers4

1

The only way to create a valid .doc file is to use Microsoft Office. It can be done from your program with OLE Automation (more on that problem here - Why are the Microsoft Office file formats so complicated? (And some workarounds)).

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ivan
  • 189
  • 1
  • 6
0

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
  • 14,257
  • 4
  • 43
  • 55
  • 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 Feb 04 '10 at 16:29
0

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.

Symbiosoft
  • 4,681
  • 6
  • 32
  • 46
0

There is another alternative to this... Practically you can call it hacking, but it is kind of a simple way as I find it for myself.

  1. Create a HTML template that looks like your document
  2. Open the HTML template using Microsoft Word
  3. Adjust the template (some things might be misadjusted)
  4. In the places where you would like to write some data/text, put some kind of marker, e.g. #offaddress#
  5. Save the document
  6. Rename the document back to HTML
  7. Open the document. The document will contain a lot of Microsoft Word document codes and stuff; don't worry.
  8. Copy the entire code and paste it in your Qt
  9. Set output to your Qt as .doc file
  10. Replace your keywords (#offaddress#) with your content
  11. Save the content to file with extension .doc

Voilà; you have your .doc file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Firesh
  • 99
  • 1
  • 2