jump to navigation

Writing from a windows form to microsoft word 2007 using VSTO November 29, 2008

Posted by fofo in general .net, MS Office, VB 2005, VB 9.0, Visual Studio 2005, Visual Studio 2008.
Tags: , , ,
trackback

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 asp.net 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 not 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. This is the empty routine.

Private Sub ThisDocument_Open() Handles Me.Open
End Sub

12) Inside this routine we need to show the form we created in the previous steps. So in the body of the routine, type

Dim myform As New wordform()
myform.Show()

13) Now we need to create a procedure (and place it in the same class file) to get the data from the form textboxes and place it in the respective cells. Type this code. It is really simple

Public Sub writeintocells(ByVal cellinput As String, ByVal flag As Integer)
Try
If flag = 1 Then
Globals.ThisDocument.Tables(1).Rows(1).Cells(1).Range.Text = cellinput
ElseIf flag = 2 Then
Globals.ThisDocument.Tables(1).Rows(1).Cells(2).Range.Text = cellinput
Else
Globals.ThisDocument.Tables(1).Rows(1).Cells(3).Range.Text = cellinput
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

14) Go to your form. Double Click on the button.In the body event handling routine type

If String.IsNullOrEmpty(TextBox1.Text) Then
MessageBox.Show(“enter a value”)
TextBox1.Focus()
Else
Globals.ThisDocument.writeintocells(Me.TextBox1.Text, 1)
End If
If String.IsNullOrEmpty(TextBox2.Text) Then
MessageBox.Show(“enter a value”)
TextBox2.Focus()
Else
Globals.ThisDocument.writeintocells(Me.TextBox2.Text, 2)
End If
If String.IsNullOrEmpty(TextBox3.Text) Then
MessageBox.Show(“enter a value”)
TextBox3.Focus()
Else
Globals.ThisDocument.writeintocells(Me.TextBox3.Text, 3)
End If

In this routine we access our document and access the procedure that we created in our document. We pass the values from the textboxes to the procedure and the procedure writes these values in the cells in the word document

15)Add another button on your form. Leave the default name. Double Click the button and in the body of the event handling routine type

Globals.ThisDocument.Save()
Globals.ThisDocument.Close()

In this routine we just save and close the document.

16) Build and run your project. Type 3 values in the textboxes in your form and bress Button1.

See the values inserted in the cells. Click Button2 to save and close the document.

Hopefully everything makes sense…

If you need the source code just email me or leave a comment!!!

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Comments»

1. Pat - January 30, 2009

using .NET 2.0 How can I change the name of an open word doc and then save it manually?

2. fofo - January 30, 2009

manually? i am sure you know how to do that manually. you mean through code…it does not make sense what you say…

3. randy - February 23, 2009

Nice tutorial… :) can you send me a copy of this one? :)
— hybridartisan (a) gmail.com

4. randy - February 23, 2009

just got another question… I was able to do this one.. i’m just wondering how can I do a search query on the word document? Because I would like to do a simple knowledge base program.. instead of making Sql as the database.. I would like to use word to make it simple..

5. microsoft point codes no surveys - May 19, 2013

Hello, I read your blog like every week. Your writing style is awesome, keep it up!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: