Token Replacement in Office documents

rahul's picture

A common situation is to prepare invoices, etc. from information in a database. Clients usually have extremely customized invoice formats, but the data to be filled in is basically the same. This article and its related code present a new (I have searched, but could not find anything related to this topic, so I believe it's new), innovative, and productive way of producing dynamic Office documents. I am using the word Office in this entire document as it does not relate solely to Microsoft Office. I have tested this approach on Open Office as well, and it works. The associated code enables to perform Token Replacement on both Office products.

Again, due to a larger community available at CodeProject, this article together with its Source Code is available if full at:

http://www.codeproject.com/KB/office/OfficeTokenReplacement.aspx

The Code for the article is also attached below.

UPDATE:

  •  (Feb 24, 2010) - Fixed a minor bug in replacement of header/footer sections.
Articles: 
.NET: 

Comments

Excellent article, i just want to thank you for good read..

By Anonymous (not verified)

This is exactly what I was looking for, gracias

By BettyBootleg (not verified)

Your blog is full of interesting articles, thanks for good read

By Anonymous (not verified)

Very interesting blog, bookmarked for the future referrence, what template do you use ?

By Anonymous (not verified)
rahul's picture

Hi, which template are you talking about??

By rahul

Hey, I found your blog while searching on Google your post looks very interesting for me. I will add a backlink and bookmark your site. Keep up the good work!

By Anonymous (not verified)

I often read your blog, i think you are good writer, keep us posting

By Bill J. (not verified)

You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

By Gagan (not verified)

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up! And according to this article, I totally agree with your opinion, but only this time! :)

By Anonymous (not verified)

if data of the body of invoice exeed the page , the new page is managed from your office token replacement?

Grazie mille Mirko

By mircome1946 (not verified)
rahul's picture

Hi Grazie, I could not understand your question completely, can you please elaborate?
Please note that Token replacement won't manage  pages for you, it simply replaces tokens. If replaced content crosses page boundaries, it would be upto the Office application to manage it.

By rahul

suppose in the invoice i have 200 product rows with code, qty, descriptions ... and so on, what can i do, using your routine to replace tokens. After filling the first page , with for example only 20 rows , how can i go on to have the others 20 pages of the invoices

tanks 

By mircome1946 (not verified)
rahul's picture

Hi mircome, sorry for the delayed response (the next reponse will take more days as I am out travelling). Well the current code does not really handle that, it would simply create as many rows in appropriate Office xml. It would be upto the office utility to detect page boundaries and display accordingly.

You can try generating with 200 product rows and see if your office utility is able to handle pagination automatically when you open the processed document. If not, you would need to go through the painful process of modifying the code to detect pagination (which itself is going to be a gray area, detecting page boundaries is gonna be complex).

An easier approach might be to analyze Office xml for page boundary tags and manually replicate the page let's say 20 times, assuming each page would have a fixed number of 20 product rows. But that also would need modifications to the code.

By rahul

Add new comment