Monday, December 1, 2008

Writing from a windows form to microsoft word 2007 using VSTO

First of all I want to thank all these people who read my posts and thank them for their encouraging comments.

In the feedack I receive I get some “complaints” that my posts are targeted towards the side of development. Well you are right. I am more knowledgeable in Asp.Net. But I promise to write more about windows forms in the future, starting from this post. And to all VB guys out there, i will try (starting from this post) to write more samples in VB, which is here to stay…

In this post I would like how easy it is to use Visual Studio Tools for Office. If you have never heard before of VSTO just google it. In a few words it is a component of Visual Studio(since Visual Studio 2005) that provides a robust, .NET-based environment for building business applications using classic Office programs like Word and Excel. So that means you do not have to learn the specific Office object models, and of course you do need VBA anymore.

Let’s show VSTO with a simple example. We will have a simple table(1 row and 3 cells) in a word 2007 (.docx) document and fill in the values of these 3 cells from a windows form.

1) Start Visual Studio 2008/2005 project.

2) From the “New Project” window choose a Office 2007 and from the templates Word 2007 Document. Select VB as the development language.Give a name to your project and Press OK

3) In the next window select “Create a new Document” and give name to your new word document, e.g “mydoc”.

4) Click “OK” to any window that asks you for access to the Office programs

5) If you have done everything right up to this point, you will be ablw to see in your visual studio window a blank word document,

6) Add a table with a single row and 3 cells from the ribbon

7) Add a new item in your project, a windows form and call it “wordform”

8) Add 3 Label controls on the form. Name them, Cell1,Cell2,Cell3.

9) Add 3 textbox controls on the form. Leave the default names

10) Add a button on the form. Leave the default name.

11) In the mydoc.vb(this is a file in my example-if you named your word, inputword, it will be inputword.vb) choose the Document.Open event.

Read More..

No comments: