views:

506

answers:

5

I have automated word document creation from templates using the "Microsoft Word 12.0 Object Library" in my project, but it is unacceptably slow. I'm looking for alternatives: I need to create a document from a template, run some macros and insert text and tables into various bookmark ranges (some text formatting options would be nice too). I only need the final document to be compatible with Word2007, but would prefer a solution easily available from .NET (other recommendations are appreciated too)...

Please advise.

+2  A: 

Have a look at Microsoft's Office OpenXML SDK. It allows you to create Word 2007 (.docx) documents programmatically without the Office applications.

To use it you will need to be familiar with the OpenXML file format. A good starting point also providing examples are the blogs of Brian Jones and Erika Ehrli.

This sample on CodeProject might also be a good start: Creation of a Word 2007 document using the Open XML Format SDK.

I also recommend you to have a look at the DocumentReflector, a tool included within the SDK, that creates the code to produce a certain Word document based on an existing document.

0xA3
Does this SDK include options for handling/running macros inside OpenXML documents? Btw: thank you for the great answer.
akosch
No, the SDK is only working on the level of the new XML-based file format. That means that you would have to rewrite your macro code in .NET.
0xA3
And can I retain macros from my templates in my newly created documents?
akosch
Yes, that is possible if your templates are in .docm/.dotm format. You will basically copy the template, and then open the copy using the SDK and populate it with content.
0xA3
+2  A: 

Options, I used up to now:

  1. Yours - COM automation: performance wasn't the biggest issue here for me, just all the ugly details of COM automation like dead processes, locked files, ...; for some tasks where an API exists and as long as only person uses the app, its still an option. Requires Word installation.
  2. Import as HTML and save as Word - works quick and better as expected, as long as your word format is simple and matches HTML markup structure quite well (e.g. headers, item lists). To simplify handling, templates have to be HTML then, so often a quick hack for a limited use case. Requires Word installation.
  3. Concatenate and process templates saved as WordML: saves you the pain of COM automation, but since WordML is just a binary format masked as XML, it's often a pain; hence some tasks are more complicated than using a Word-API. Nevertheless I prefer it over 1. Does not require a Word installation.
  4. As in previous answer, OpenXML SDK: just starting to use this, seems to be a good compromise of 1 and 3: work on XML level or on API level - whatever fits best for a certain feature. Does not require a Word installation.
Frank Bechmann
A: 

other recommendations are appreciated too

The Java equivalents to the OpenXML SDK are docx4j (my project), and POI.

Using docx4j, you can inject macros into an existing docx, or work from a dotm (as divo suggests).

plutext
A: 

Have a look at Aspose.Words either for .NET or Java.

romeok
A: 

I got very very valuable information on Document Creation thanks dude. I am glad to thank to you.

Document Comparison