Visual Studio Tools for Office
Once you have determined that items have been selected, iterate through the SelectedItems collection of the NameList object and add a row to the table for each item, setting the first cell in the row to the name of the company, and putting some placeholder text in the second cell. Your final subroutine should look like the following listing:
Private Sub InsertNames_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles InsertNames.Click
If NameList.SelectedItems.Count > 0 Then
For Each NameToInsert As ListViewItem In NameList.SelectedItems With Globals.ThisDocument.Tables(1)
.Rows.Add()
.Rows(.Rows.Count).Cells(1).Range.Text = NameToInsert.Text
.Rows(.Rows.Count).Cells(2).Range.Text = “Put information here” End With
Next
Else
MessageBox.Show(“You must first select an entry in the list”) NameList.Focus()
End If End Sub
11.You’re almost done. The only tasks left are to connect the CompanyNameActions component to the document and write some code to populate the ListView control. Open the ThisDocument.vb code module and define the CompanyNameActions object at the top of the class. Again, because you don’t need to intercept any events, just define it as a regular object:
Private TableAreaPane As New CompanyNameActions
12.Rather than being visible at all times, the TableAreaPane object will only be shown when the user is currently editing the table in the document. To determine when the user is within a bookmarked area, you can use the Selected and Deselected events. Add event handler routines for both events to add and remove the TableAreaPane to the ActionsPane.Controls collection as appropriate. The following code shows you how this can be achieved:
Private Sub TableArea_Selected(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.SelectionEventArgs) Handles _ TableArea.Selected
Me.ActionsPane.Controls.Add(TableAreaPane)
End Sub
Private Sub TableArea_Deselected(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.SelectionEventArgs) Handles _ TableArea.Deselected
Me.ActionsPane.Controls.Remove(TableAreaPane)
End Sub
13.This will display the TableAreaPane object when the user places the cursor inside the bookmarked table of the document, so the only task left is to populate the ListView. You’ll do this by directly accessing the NameList control within the TableAreaPane object from the document, which demonstrates how you can access properties and controls in the Actions Pane components attached to a document.