Click here to Skip to main content
13,287,834 members (47,124 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 15 Jan 2009

Reporting: Embed dotnet code in MS Word files

, 15 Jan 2009
Rate this:
Please Sign up or sign in to vote.
Simple reporting engine using MS Word files as templates


This is a simple reporting library. It allows developer to prepare report templates from MS word files. In MS word file, developer can embed code directly.  


I got the idea for this library from excellent Ruby Documatic project. 

Using the code 

There are two steps to create your reports from MS Word templates. First open new MS Word document and edit it. Use ### for code placeholder and $$$ for display placeholder. Code between begining ### and ending ### must be placed in the same paragraph. $$$ should be one-liner. I got an example of small template below. Then save document as MS Word XML document. You have to use MS Word 2007. 


So let's review what's going on. There are some import statements on the top of the page.
Then there is a first table that is going to display 10 rows, right?
And second table is looping throug some test animal collection. You might observe that there is "data" variable. This variable is useful for sending your specific data into report. In Word template, just cast it to whatever type necessary and use it. 

The second step involves calling report:

      Dim data As New List(Of Object)
      data.Add(New Animal("Elephant", 1000))
      data.Add(New Animal("Tiger", 300))
      data.Add(New Animal("Crocodile", 250))
      data.Add(New Animal("Dog", 40))
      data.Add(New Animal("Chicken", 1))
      data.Add(New Animal("Dragon", 10000))
      Dim nd As New NDocs.Generator
      nd.Generate("xmlfile2.xml", "final2.xml", data)

This is final result: 


Easy, right? Well, this library is still in very alpha phase, so it is possible that not all templates will work. Just keep in mind that you should put whole code between ### in one paragraph (use shift-enter), and $$$ are one-liners.  

How does this library works?

Actually, quite simple. It is 8 KB after all.

So here are the principles: 

1. Developer edits MS Word file (or later customer modifies it, or even customer sends first version and developers add tags) 

2. The MS Word file is saved as XML document  

3. Generation of final (output) file goes like this: 

3.1 XML parser extract code fragments (###) and original MS Word tags 

3.2 all tags are combined into dynamically generated source, which is then run-time compiled.. The result is new run-time, in-memory type (class), which can be instantiated and invoked.

3.3 this new type is then invoked via reflection 

3.4 the result is simply written to output file 

3.5 no MS Word APIs are needed

I am looking forward for your suggestions/opinions.    


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead Apida d.o.o.
Slovenia Slovenia
I am a programmer.

You may also be interested in...


Comments and Discussions

GeneralSomething new Pin
ashu fouzdar22-Jan-09 22:42
memberashu fouzdar22-Jan-09 22:42 
GeneralMy vote of 1 Pin
Dave Kreskowiak15-Jan-09 4:39
mvpDave Kreskowiak15-Jan-09 4:39 
GeneralRe: My vote of 1 Pin
ivan.bolcina15-Jan-09 6:22
memberivan.bolcina15-Jan-09 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 15 Jan 2009
Article Copyright 2009 by ivan.bolcina
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid