March 5, 2021
Hot Topics:

Using Visual Studio Tools for Office in Visual Studio 2008

  • By M. Rajesh
  • Send Email »
  • More Articles »

Do this for all the remaining controls in the panel so that each column is bound to its respective column in the Customers table.

Now, you will change to the .NET environment and then do some real coding to display the list of customers in the panel.

Double-click the panel and you will be switched to the code behind window.

Enter the following code so that the navigator populates data from the Customers table.

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

Now, go back to the Thisdocument.vb form; this is where you will write the complete code to display data in the navigator as well as populate the word document. The code for the complete class is seen below. Note that you will have to make a reference to Microsoft Office Word 12.

Imports WordTools = Microsoft.Office.Tools.Word

Public Class ThisDocument

   Private customerPane As Navigate

   Private Sub ThisDocument_Startup(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles Me.Startup
      ' Add the CustomerNavigator control to the actions pane
      customerPane = New Navigate()

      'Bind the content controls to the data source

   End Sub

   Friend Sub LockContentControls(ByVal locked As Boolean)
      ' Iterate the host controls on the document. If the host
      ' control is the type PlainTextContentControl, then lock the
      ' contents and the control itself.
      For Each ctrl As Object In Me.Controls
         If ctrl.GetType() Is GetType( _
         WordTools.PlainTextContentControl) Then
            Dim plainContent _
               As WordTools.PlainTextContentControl = _
               TryCast(ctrl, WordTools.PlainTextContentControl)
            If plainContent IsNot Nothing Then
                  plainContent.LockContentControl = locked
                  plainContent.LockContents       = locked
               Catch ex As Exception
                  Exit For
               End Try
            End If
         End If
   End Sub

   Private Sub BindContentControls()
      For Each ctrl As Object InMe.Controls
         If ctrl.GetType().Equals(GetType( _
         WordTools.PlainTextContentControl)) Then
            Dim contentControl As WordTools.PlainTextContentControl
            contentControl = TryCast(ctrl, _
            If contentControl IsNot Nothing Then
               If contentControl.Tag <> String.Empty Then
                     contentControl.DataBindings.Add("text", _
                        customerPane.CustomersBindingSource, _
                     MessageBox.Show( _
                        "Unable to bind content control.")
                     Exit For
                  End Try
               End If
            End If
         End If
   End Sub

End Class

Now, review the above code and see what it does. The BindContentControls function binds the document's XML tag to each column of the navigation panel.

The lockContentControls function locks the entire navigation panel so that the contents cannot be manipulated, except when clicking the Previous and Next rows of the Navigation Panel.

Now, go back to the Navigate.vb form and enter the following code so that the panel is unlocked when navigating through the controls.

Private Sub BindingNavigator1_Move(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles _
   BindingNavigatorMoveLastItem.Click, _
   BindingNavigatorMoveFirstItem.Click, _
   BindingNavigatorMoveNextItem.Click, _
      ' When the current item in the binding navigator changes,
      ' unlock the content controls before data binding
End Sub

That is all is required to run the application. Press F5 and run the application. You will see that Word 2007 launches and after a while you will see that the Invite template loads along with the Action Pane. Inside the action pane, you will see a navigation panel that allows you to scroll through each customer as found in the Customers table.

Page 6 of 7

This article was originally published on April 28, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date