Visual Basic Concepts

Adding a Property to the UserDocument

As with the UserControl object, you can create public properties for UserDocument objects. When you create a public property, you expose the property so other applications can set or get its value. In the ActXDoc project, you'll create a single property that can be accessed by either another ActiveX document or by a form. In this procedure, you will also add a code module to the project to contain a single global module.

Note   This topic is part of a series that walks you through creating a sample ActiveX document. It begins with the topic Creating an ActiveX Document.

To add a property to FirstDoc

  1. First add a code module to the project. On the Project menu, click Add Module to open the Add Module dialog box. Then double-click the Module icon to add a new code module to the ActXDoc project.

  2. In the Properties window, double-click Name and change the name of the module to mGlobal.

  3. In the Project Explorer window, double-click the module icon and add the following code to the Declarations section:

    ' Object variable for FirstDoc
    Public gFirstDoc As FirstDoc
    

    This global variable will contain the object reference that links the FirstDoc and SecndDoc ActiveX documents.

  4. In the Project Explorer window, double-click the FirstDoc icon.

  5. On the Toolbox, double-click the TextBox icon to add a new TextBox control to FirstDoc. Set its properties according to the following table.

Text1 property Value
Name txtFirstDoc
Text (nothing)
  1. Position the TextBox control below the first TextBox already present on the UserDocument, as shown:

  2. Double-click the FirstDoc designer to open its Code window.

  3. On the Tools menu, click Add Procedure.

  4. In the Add Procedure dialog box, type "strDocProp" into the Name box, and click the Property option to create a public property; then click OK.

  5. Visual Basic will add code for Property Get and Property Let procedures to the module. However, modify the code as shown:

    Public Property Get strDocProp() As String
       ' Note: in the line above, change the return type
       ' of the property from "As Variant" to
       ' "As String."
       strDocProp = txtFirstDoc.Text
    End Property
    
    Public Property Let strDocProp(ByVal _
    NewStrDocProp As String)
       ' Note: in the line above, change the argument
       ' type from Variant to String.
       txtFirstDoc.Text = NewStrDocProp
    End Property
    

    The code above exposes the strDocProp property as a public property of the FirstDoc ActiveX document. In other words, it delegates to the Text property of the TextBox control the work of displaying and storing the string. Now that the property is public, you can pass its value to the SecndDoc object. You will do this by modifying the code for the Go Next command button.

  6. In the Code window, modify the Click event of the cmdGoNext button to include a Set statement with the global object gFirstDoc. When you set the object variable to Me, you create a reference to the FirstDoc ActiveX document. This reference can then be used to access the public properties and methods of the document.

    Private Sub cmdGoNext_Click()
       ' Note: the following path may not correspond to
       ' the actual path to the SecndDoc.vbd file on
       ' your machine.
       Set gFirstDoc = Me ' <-- Add this line.
    
       HyperLink.NavigateTo _
       App.Path & "\SecndDoc.vbd"
    End Sub
    
  7. On the Project Explorer window, double-click the SecndDoc icon to bring its designer forward.

  8. Double-click the SecndDoc designer to open its Code window and add the following code to the UserDocument object's Show event.

    Private Sub UserDocument_Show()
       If Not gFirstDoc Is Nothing Then
          lblCaption.Caption = gFirstDoc.strDocProp
          Set gFirstDoc = Nothing
       End If
    End Sub
    

    In the code in step 11, you set the global object variable to the FirstDoc document. The code in step 13 tests to see if the global variable is set to an object. If it is, then the public properties of the ActiveX document are available, and the caption of the label is set to the strDocProp property from the FirstDoc document. Immediately after setting the Caption property, the global variable is destroyed (set to Nothing).

    Note   "It is bad practice to allow the global variable to retain a reference to the FirstDoc UserDocument; the reasons for this are covered in the "Building ActiveX Documents."

  9. On the File menu, click Save Project, and save the code module as mGlobal.bas.

Running the Project

  1. Press F5 to run the project.

  2. Type the path of the FirstDoc.vbd in the Address box of Internet Explorer.

  3. Type something distinctive into the new TextBox control such as "HTML was never this easy."

  4. Click Go Next. The text that was in the TextBox control will appear in the label of the SecndDoc document.

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX document.

To See
Go to the next step Saving Properties to the PropertyBag
Start from the beginning Creating an ActiveX Document