Welcome to MSDN Blogs Sign in | Join | Help

Adding Controls to a Document from the Actions Pane

I responded to a question in the VSTO Forum about how to add a TextBox control to a Word document by clicking a button on the ActionsPane. I thought I'd share the code here as well.

This code adds the textbox to the current selection to give the user the opportunity to choose where in the document the textbox is added. Control names must be unique, so I've incremented the name of each control added by counting the total number of controls in the document.

If you have a button on a user control that you add to the actions pane, you can add the following code to the user control:

Public Class UserControl1

    Dim ControlCounter As Integer = 0

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

       Dim Selection As Word.Selection = _
          
Globals.ThisDocument.Application.Selection

       Dim ControlName As String = "Control" & _
           ControlCounter.ToString()

       ControlCounter += 1

       Globals.ThisDocument.Controls _
          .AddTextBox(Selection.Range, 100, 50, ControlName)

    End Sub

End Class

 

--Kathleen

 

Published Saturday, October 14, 2006 7:45 PM by kmcgrath
Filed under:

Comments

No Comments
Anonymous comments are disabled
 
Page view tracker