Token Replacement in Office documents RRS feed

  • Question

  • A common situation is to prepare Invoices, etc. from information in the database. Clients usually have extremely customized Invoice formats, but the data to be filled in is basically the same.

    I was trying to create a word document, with Tokens of the form [$TokenName$] in it, and replacing the tokens with actual text programatically. However, it was not as easy as I thought.

    Word 2007 splits up the token into multiple parts, depending upon regions to be checked for spellings. That makes the scenario almost impossible.

    Can there be a convinient solution for token replacement??

    Also, it would be helpful to be able to add rows to existing column based on a row template. In the same Invoice scenario, we leave a single row for Goods being delivered, and replicate that row replacing the Tokens for all Goods in the Invoice.

    Saturday, December 13, 2008 10:02 AM

All replies

  • Hi Rahul,

    A quick question: where is this invoice templates from?

    If you prepared this templates or you can modify them, I hava an idea:
    You modify the templates so that the token texts are bounded in a single Text under a Run, then, you can use one line of LINQ to get a collection of the Run objects whose inner text is exactly equal to the token string.

    If I misunderstood your question, please speak out so that we could try to find the right solution for your problem.

    Tuesday, December 16, 2008 8:07 AM
  • I was waiting so patiently for a reply here.

    Firstly, you understood me correctly.

    Secondly, I initially thought of doing so (modifying manually). But that is infeasible, because I was looking for a generic solution. I am preparing a product, that I might distribute to multiple clients. And it would be a gross loss of productivity to modify templates of all of them manually.

    I am now resorting to some sort of regex to clean-up all xml between the start & ending tags of the token I am using. But this would take time to get a solution. In the meantime, can you suggest an alternative???
    Tuesday, December 16, 2008 8:25 AM
  • With no reply here, I have come up with my own solution.

    As I believe that this situation & its solution I have created are both generic, I have uploaded my solution together with some explanation on codeproject.

    Anyone interested can have a look at:
    Tuesday, December 23, 2008 9:23 AM