2
votes

We have inherited a ton of code written in C# on .NET 2.0 in Visual Studio 2005. The backend database is MS SQL server 2000. We have migrated the code to C# on .NET 4.0 in Visual Studio 2010 and can compile and build. The problem is the old code used the Microsoft.Interop.Office.Word library to allow us to generate server-side reports using mail merge and macro-embedded templates. We now understand that Visual Studio 2010 does not support server side VBA or C# MS Word application work. So we have downloaded Open XML 2.0. The documentation is beyond frustrating. And the myriad posts we find on the web speak to document creation from scratch, little about mail merging or running enabled macros in .dotm files, etc and most of them deal with Open XML 1.0.

Can I convert a .dotx file to a .xml file in MS Word, then just use C# and the Open XML API to load up that .xml file and embed the variable data pulled from the database? It seems absolutely crazy to construct an EXISTING .xml file (that is a 30 page report) dynamically in code each time the report is run. Am I missing something?

If someone has successfully done this, and example with C# code would be really helpful. Thanks, Emily

1
What makes you think it doesn't support MS word application work?John Saunders
不懂: ton of code written in C# on .NET 2.0 in Visual Basic 2005Todd Main
The problem is we want to migrate to VS2010 using .NET 4.0 on SQL 2008 R2. Microsoft says they no longer support server-side Word managegment through VBA or C#. Bangalore told me to rewrite everything in Javascript or use openXLM. The documentation on openXML is hideous.Emily Campbell
they have never supported server-side Word access. Never. It has never been safe, and usually doesn't work.John Saunders

1 Answers

2
votes

You can try a really really simple find/replace method.

  1. Create your template as a .docx file and save it on the server
  2. Change all your mail-merge fields with easy to find and parse placeholders that are not going to appear int he document normally and will not require XML encoding. For example: {{FirstName}}.
  3. Create a new blank .docx (zip) file for each document you want to generate
  4. Read each file from inside the template .docx file
  5. As you read the source file, search for and replace your tokens as appropriate
  6. Send file to client

I haven't worked with WordML myself so I don't know if there are gotchas that would prevent the above from working, but based on my understanding of Open XML and use of SpreadsheetML, it should work.

Working with OpenXML is more complicated than Office Interop. The Office Interop API was much closer to the Office UI and thus familiar and easier to use. The Open XML API is a very light wrapper on the core XML and is almost exactly the same as dealing with the XML directly. It's more complex without quite the same automatic familiarity.

However, OpenXML is much better than office interop. Office interop has never been safe for server use. Think of this as a good time to get rid of your old ticking time bomb.