Options for automating the creation of Microsoft Word 2007 documents

I have an automatic creation of a Word document from templates using "Microsoft Word 12.0 Object Library" in my project, but this 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 different ranges of bookmarks (some text formatting options would be nice too). I only need the final document for compatibility with Word2007, but would prefer that the solution be easily accessible from .NET (other guidelines are also appreciated) ...

Please advise.

+2


source to share


4 answers


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

To use it, you will need to be familiar with the OpenXML file format. Examples from Brian Jones and Eric Earley are also good starting points .



This example in CodeProject might also be a good start: Creating a Word 2007 Document Using the Open XML Format SDK .

I also recommend that you take a look at DocumentReflector, a tool included in the SDK that generates code to create a specific Word document based on an existing document.

+2


source


The options I've used so far:



  • Yours is COM automation: performance isn't the biggest issue 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 a person uses the application, its still an option. Requires Word installation.
  • Import as HTML and Save as Word - Works faster and better as expected as long as your word format is simple and matches the HTML markup structure quite well (e.g. headings, lists of items). Templates must be HTML to simplify processing, so often a quick hack for limited use. Requires Word installation.
  • Concatenation and process templates saved as WordML: save you COM pain, but since WordML is just a binary format disguised as XML, it is often a pain; therefore, some tasks are more difficult than using the Word-API. However, I prefer it over 1. No need to install Word.
  • As in the previous answer, OpenXML SDK: just started using this seems to be a good compromise 1 and 3: working at the XML level or API level - whatever is appropriate for a particular feature. No Word installation required.
+2


source


other recommendations are also appreciated

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

Using docx4j you can embed macros in existing docx or work with dotm (as divo suggests).

0


source


Officewriter is another option.

The OfficeWriter API is a .NET library that allows you to create, manipulate, and read Excel and Word documents from your own application.

0


source







All Articles