Partager via


Simulating Application-Level Programmable Task Panes

There has been a lot of talk in the newsgroups about the need to have an application-level programmable task pane in Word solutions. Using Visual Studio 2005 Tools for Office (VSTO), you can create only document-level actions panes, meaning that the task pane is only available to the document for which the actions pane was created. However, if your goal is to use the functionality of the task pane on *any* existing document, you can create an application using VSTO that simulates an application-level task pane.

 

Let's say you have an actions pane that enables you to add boilerplate text to the current cursor location, and you want to be able to use this actions pane with any document you have on your system. If a user opens a new document, he would not have access to the solution that contains the actions pane. One way to work around this is to add a browse button to the actions pane solution so that a user can select a document to open. Instead of actually opening the selected document, you can insert it into your existing solution. The actions pane would then be available for that document (and any other document the user selects).

 

1.  Create a Word Template project and add an ActionsPane control to your project.

 

2.  Add a TextBox control named TextBoxFileName, and a button named ButtonBrowse to the ActionsPane control so that the user can navigate to the document to be opened.

 

3.  Add an OpenFileDialog control to the ActionsPane control.

 

4.  Add the following code to the Click event of the ButtonBrowse control to insert the selected document into your document. You'll want to make sure that the current document is empty before inserting the new document.

 

Private Sub ButtonBrowse_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    ButtonBrowse.Click

    If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
         Me.TextBoxFileName.Text = Me.OpenFileDialog1.FileName

 

        ' TODO Check if there's text in the document
        ' and whether the user wants to save it before
        ' inserting the new document.

        ' Delete the contents of the current document and
        ' insert the new document.
        Globals.ThisDocument.Range.Delete()
        Globals.ThisDocument.Range( _
            0, 1).InsertFile(Me.TextBoxFileName.Text)
    End If

End Sub

 

In this way you can use the same actions pane solution for multiple documents by using a VSTO-enabled template that takes its content from other documents. You'll need to provide functionality to save the document (with or without the task pane) before deleting the contents and inserting a new document.

 

An alternative to this solution is to attach managed code extensions to existing documents. I'll talk about this more in another post.

 

--Kathleen McGrath

 

-----
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.