Create Word 2010 Document Programmatically

笑着哭i 提交于 2019-12-21 17:42:34

问题


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.


回答1:


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!




回答2:


You have lots of options:

  • automate Word
  • do it as a macro (dotm)
  • create your document using Open XML SDK
  • VSTO

You are probably best off doing it using a macro first, though this is old VB6. The techniques you quickly learn there translate pretty well into the other approaches. Open XML SDK has the virtue of not needing Word to create the document.



来源:https://stackoverflow.com/questions/4047782/create-word-2010-document-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!