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.

Help/Advice appreciated.

Answers


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:

http://support.microsoft.com/kb/316383

http://msdn.microsoft.com/en-us/library/ee861527.aspx

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!


Need Your Help

Can I specify my explicit type comparator inline?

c# .net linq lambda

So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and manipulate data, thanks to all the neat functions supplied with LINQ. Sometimes, I need to compare user-defined types that don't

c# generic Enum class operations

c# generics enums

I'm trying to achieve the following goals: having a method that will get a generic Enum and check if that enum is from the Enums defined in the same class and having another method that is returnin...