Create Word 2010 Document Programmatically - c#

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.

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!

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.

Related

VSTO Office Word Add-In Development Overview

C# expert and ASP.NET developer at heart, I find myself assigned now to an Office Add-In project. Luckily I have worked on several Excel Add-Ins in the past, but it's all blurry in my mind and I never had a chance to learn it in a well structured manner.
I was surprised to notice that SO and the whole Internet is lacking proper documentation for VSTO, Word DOM, OpenXML, etc. Lacking might be an exaggeration, but it's not abundant or well organized, that's for sure.
So what I'm looking for basically it's an overview, jump-start, or cheat-sheet, however you want to call it, containing a high level summary of the important parts of Office development.
PS: I will post my findings as well during my study.
My 2 cents
1) For latest WordArt in word, you would need OpenXML data to insert in Word. For Excel\Powerpoint, things are easier, you can just use interop.
2) Don't always trust the documentation. If you get an int instead of a bool return value, its mostly a value from MsoTriState. e.g. Bold property should return bool as mentioned here. But, instead it returns int. So, you have to cast it like "(MsoTriState)returnedBoldValue" to figure out what that integer actually means.
3) Installation can be a pain, consider using ready-made products like Advanced Installer(I found it cheaper than others). Others might be good or better as well.
4) Table styling can be a pain. Here is a list of the weird IDs you need to use it for Powerpoint.
5) TextFrame2 is still not valid for Word(even if the documentation might say otherwise). Its only available to Excel and Powerpoint. For Word, you have to use TextFrame.
6) WPF is not supported. But, I was able to get it working just fine after some research. You need to make sure to load resource dictionaries and they work fine as well.
7) Window Association was a bit of an issue(Parent - Child). WindowInteropHelper is your friend there.
For now, I will stop here. I will post some links later.
Document Level Architecture
Document
Template (essentially the same as Document from VSTO's perspective)
"Contained" by the Document.
Document contains the relevant information required to load the Add-In. Does not use Registry.
Application Level Architecture
Add-in
Directly plugged into and loaded by Office. Agnostic to Document. It is registered via Registry keys.
Ribbon
Supported by Visual Designer. It handles state automatically for you.
Ribbon X
It allows a much better customization. It's XAML like. No Visual Designer. It doesn't handle state. You need to handle state on your own, using callbacks.
Word DOM - Document Object Model
Application
Documents
StoryRanges
Range
Styles
Style
Windows
Window
Panes
Pane
It is nothing special, just another DOM, quite similar to the HTML one. However, it will take some time to get used with it and the information on the Internet is not as abundant as one might hope.
Manipulating the DOM
Directly
VSTO Controls: Windows Forms Controls, Host Controls
Windows Forms Controls
Powerful (supports .NET databinding for example) however they are not really part of Office so they will feel like a nut in a wall.
Host Controls (wrappers around the Interop Word objects)
Bookmark Control - old method, move on, nothing to see here.
XML Node & XML Nodes - deprecated. Removed in USA due to lawsuit.
Content Controls - The future! Support real data binding to hidden XML data field in the XML parts of the document.
Deployment
Click-Once - simple and fast. It adds the Registry entries for you. But it doesn't allow much customization. For example you can have a Click-Once project installed only to the current user.
Windows Installer, Installshield, etc. You need to add the .manifest and the .vsto files to the output. You need to add the registry keys.

Connecting To A Website To Look Up A Word(Compiling Mass Data/Webcrawler)

I am currently developing a Word-Completion application in C# and after getting the UI up and running, keyboard hooks set, and other things of that nature, I came to the realization that I need a WordList. The only issue is, I cant seem to find one with the appropriate information. I also don't want to spend an entire week formatting and gathering a WordList by hand.
The information I want is something like "TheWord, The definition, verb/etc."
So, it hit me. Why not download a basic word list with nothing but words(Already did this; there are about 109,523 words), write a program that iterates through every word, connects to the internet, retrieves the data(definition etc) from some arbitrary site, and creates XML data from said information. It could be 100% automated, and I would only have to wait for maybe an hour depending on my internet connection speed.
This however, brought me to a few questions.
How should I connect to a site to look up these words? << This my actual question.
How would I read this information from the website?
Would I piss off my ISP or the website for that matter?
Is this a really bad idea? Lol.
How do you guys think I should go about this?
EDIT
Someone noticed that Dictionary.com uses the word as a suffix in the url. This will make it easy to iterate through the word file. I also see that the webpage is stored in XHTML(Or maybe just HTML). Here is the source for the Word "Cat". http://pastebin.com/hjZj6AC1
For what you marked as your actual question - you just need to download the data from the website and find what you need.
A great tool for this is CsQuery which allows you to use jquery selectors.
You could do something like this:
var dom = CQ.CreateFromUrl("http://www.jquery.com");
string definition = dom.Select(".definitionDiv").Text();

Calculations in excel using .net

Hi Stackoverflow Users
I was recently assigned to rewrite a excel document my company uses a lot. Originally it was written in matlab, but to easier distribute it, they migrated it to excel. It was done very bad, and therefore it worked on some computers and not on others. So it was rewritten to vba. But now they want it to be able to do very complex calculations like Monte Carlo simulations. therefore we wish to write it in c#.net for performance and development reasons. the last week i have tried and tried to learn how it was best done. I found several possible solutions, but i have not found a single way to do it which meets my 2 simple demands:
The user doesn't have to go to any settings and add something, if it have to be done, it need to be done in code
The calculations is done at the press of a button, so i need to be able to receive the event
I have spent so much time on the web, but i don't know exactly what to search for, so it all just seems like a big wall of unexplained, complex com-magic.
So what are your suggestion? Is there any solution which meet those demands? Any suggestions, references or any other help will be deeply appreciated.
Excel doesn't natively support C# in macros, so you're not going to be able to meet your first demand in a stand-alone spreadsheet. If you're willing to install an add-on into Excel, you might want to consider Excel-DNA. This would allow you to compile a .NET DLL containing the functions you want to add in to Excel.
Receiving an event from a button click is simply a case of adding a button control onto a sheet, and assigning a macro to it. This macro (e.g. a VBA function) will be run on demand when you click the button.

How to write to word document (in a particular format ) From C# windows forms application

I am developing a C# windows application , which captures the user's inputs(in the form of numerous textboxes, radio buttons and drop downs).
I need to write the captured response from User to a word document which fits in the word document which has a predefined template.
Could some one let me know the best way to approach that?
Thanks in advance,
Vijay
Depending upon how complex your needs are and the depth of your pockets there is a great product called Aspose.Words. We use their suite of products for generating office documents at work and they're really easy to work with. It's unfortunately a pretty expensive product so unless you are very committed to your product it might not be right for you.
Otherwise you might want to check out Microsoft's Open XML SDK.
[I have no affiliation with the company, aside from having used their products.]

Using MS Word Templates to allow a user to fill out a report

I'm trying to create create a simple word template on the fly which would just contain text and a few text input fields and then open a new word document based on that template for the user to fill out. I don't think this would be too difficult using Word automation and C#, but I've never worked with Word's COM interface before.
Any links to examples or fresh examples on how to create a simple template with some text and a text input field and then how to open a document based on that template in C# would be greatly appreciated.
Thanks in advance!
I think "Office Interop" is the keyword you are looking for.
You'll find a starting point here: http://support.microsoft.com/kb/316384
Several drawbacks involved (for instance, installed Office on client machine required; not advisable to be run as a server process), but might do the job for your requirement.
I have done a fair amount of work with Word automation - though not really so much with tempates... My advice would be first of all to have a look at the documentation that ships with the product - which is a simple matter of editing a word macro, and pushing F1 in the code editor. I have found that to be a rich source of information...
The other piece of advice I would give you is to try recording a macro in Word that covers any task you want to do, and then inspecting the code that is generated. This has proven invaluable to me in getting stuff up and running quickly...
Hope this helps...
Martin.

Categories

Resources