Create Word 2010 Document Programmatically
Every week I produce a word document with some copy, tables and charts from various sources on our network.
It can take a bit of time and sometimes I make mistakes or forget stuff that should go in. I want to automate this process as much as possible.
So basically I want to make a Word Template or Console App that when I open/run it it goes off and collects all this stuff and links it into the various parts of the document.
Assume I have to insert:
- Some copy from another Word document
- A PNG (pie chart)
- Create a table from a CSV file
- Have a standard Header and Footer with page numbering
I usually make a few changes to the copy in the document to highlight the highlight from the week.
Then I check it into a SharePoint where network users can open and view it.
I figure the thing to do is crack open VS2010 and make a Word Template project. I have never done this before and I wondered what traps there are or if its even an appropriate way to solve my problem.
My other option would be some power-shell but i'm no expert there either.
I'd be pretty comfortable writing console apps so I might end up doing it that way.
I would approach this problem by breaking down the individual steps as you would perform them if you were sitting in front of a blank Word document. Then automate it using the object model. There's a little bit of a learning curve, but once you get past that you'll be addicted to automating Office. Office is quite a powerful platform. Most of what you can achieve via the User Interface can be done programatically. I do quite a bit of automation with Excel. The code is fairly intuitive...its things like worksheet.Range["A1"] = "abcd" (setting cell A1 = "abcd").
here are some pointers:
If you go through the tutorial in the first link, you'll get the gist of it. What's great is that you can use the debugger to step through your lines of code. As each line executes, you can see the results on the Word document. I've never used a Word Template project, so I can't speak for the pros/cons there. Going the object model route, you just have to articulate what you want to have happen, break it down into the individual steps as you would perform them, and then code it up. If you get stuck, likely someone else has blogged/posted about how to make something happen. Google will find a solution very quick. Good luck!