Without seeing the code, its hard to determine what the problem is. I have an example of doing this in my book (VSTO for Mere Mortals), but since the book hasn't been published yet, I'll share a bit of the code here for you.
Things to keep in mind are:
- Rather than using ActiveDocument, you should use ThisDocument, which you can access from the user control by using the Globals class.
- 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
I hope this helps!
Kathleen McGrath http://blogs.msdn.com/kathleen
This posting is provided "AS IS" with no warranties, and confers no rights. |