Starting the Form

Now that you have created your form, you are ready to launch it when the user creates a new document from the template. The New event of the document fires when a new document instance is created. Right-click ThisDocument.vb in Solution Explorer, and click View Code. In the Class Name drop-down box (left), select ThisDocument Events. The Method Name drop-down box (right) now displays all the events of the Document object.

Select New in the right drop-down to create an event handler to handle the New event. Type the code in Listing 12.4 in the New event handler. This code is very similar to the VBA code. The first thing you change is the way you reference the Document object. Instead of using Active-Document, you use the Me keyword. Also, the way you launch the Windows Form is a little different. Because Visual Basic 2005 is an object-oriented language, you create a new instance of the frmMain object.

Listing 12.4. Displaying a Windows Form when a new document is created Public Class ThisDocument

Private Sub ThisDocument_New() Handles Me.New Me.SpellingChecked = True Me.GrammarChecked = True Me.ActiveWindow.Caption = _

Me.ActiveWindow.Caption & " - Memo" Me.ActiveWindow.View.ShowFieldCodes = False

' Show the UserForm. Dim frmMain As New frmMain frmMain.Show() End Sub

Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub

End Class

0 0

Post a comment