Windows Phone Developers

Tuesday, July 29, 2008

Create Custom Task Panes for Word using VSTO

How to Create a Word Addin using Visual Studio

Creating VSTO Word Addins in using Visual Studio is a simple process

Select Project - -> New Project - -> Office - ->Select the Version of Office (Office 2007, Office 2003) - ->Word 2007/2003 Addin


VSTO Word Addin

This will create a solution as shown below. A new class will be added with Startup and Shutdown methods



VSTO Startup & Shutdown Methods

To add a custom taskpane, insert an user control and add required controls on it and use the following code


VB.NET User Control
VSTO CustomTaskPane Designer

Imports Word = Microsoft.Office.Interop.Word

Public Class ThisAddIn

Private oUC As New UserControl1

Private oTaskPane As Microsoft.Office.Tools.CustomTaskPane

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

For i1 = 1 To Me.CustomTaskPanes.Count

Me.CustomTaskPanes.Remove(Me.CustomTaskPanes(i1))

Next i1

Add_Task_Pane()

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub

Public ReadOnly Property MyTaskPane() As Microsoft.Office.Tools.CustomTaskPane

Get

Return oTaskPane

End Get

End Property

Private Sub Add_Task_Pane()

oUC = New UserControl1 ' New Object of TaskPane (UserControl)

oTaskPane = Me.CustomTaskPanes.Add(oUC, "VBADUD Sample TaskPane", Me.Application.ActiveWindow)

oTaskPane.Visible = True

End Sub

Public ReadOnly Property WordDoc() As Word.Document

Get

Return Me.Application.ActiveDocument

End Get

End Property

End Class

To get the document information on the Label use the following code

Public Class UserControl1

Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = Globals.ThisAddIn.WordDoc.FullName

Label1.Update()

End Sub

End Class

WordDoc is retrieved from the property exposed by the Addin class


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment