Create Word 2010 Document Programmatically

Every week I create a document with several copies, tables and diagrams from various sources on our network.

This may take a little time, and sometimes I make mistakes or forget things that should come in. I want to automate this process as much as possible.

So basically I want to make a Word Template or Console App, when I open / launch it, it shuts down and collects all this and links it to the various parts of the document.

Suppose I need to insert:

  • Some copy from another Word document
  • PNG (pie chart)
  • Create table from CSV file
  • You have a standard page header and footer

I usually make a few changes to the copy in the document to highlight the highlight from the week.

Then I test it on SharePoint, where network users can open and view it.

I suppose what needs to be done is to open VS2010 and create a Word Template project. I had never done this before, and I wondered what traps there were or even its suitable way to solve my problem.

My other option would be some nutrition shell, but I'm not an expert either.

It would be very convenient for me to write console applications so that I can do so.

Help / advice appreciated.

+5
source share
2 answers

I would approach this problem by breaking up the individual steps the way you completed them if you were sitting in front of an empty Word document. Then automate it using the object model. There's a bit of a learning curve, but once you're done, you'll be addicted to Office automation. Office is a fairly powerful platform. Most of what you can achieve through the user interface can be done programmatically. I have quite a bit of automation with Excel. The code is pretty intuitive ... its stuff, like a worksheet. Range ["A1"] = "abcd" (installation cell A1 = "abcd").

Here are a few pointers:

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

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

If you follow the tutorial at the first link, you will get its essence. What sets this apart is that you can use the debugger to execute your lines of code. As each line runs, you can see the results in a Word document. I have never used a Word Template project, so I can’t speak for the pros and cons. Moving along the route of the object model, you just need to formulate what you want, break it into separate steps, how you performed them, and then encode. If you're stuck, probably someone else has posted / posted information on how to do something. Google will find a solution very quickly. Good luck

+4
source

You have many options:

  • automate word
  • do it like a macro (dot)
  • create your document using the Open XML SDK
  • Vsto

You will probably do this best with a macro, although this is an old VB6. The methods that you learned quickly go well into other approaches. The Open XML SDK has the fact that Word is not needed to create a document.

0
source

All Articles