Share via


Visual Basic Concepts

Using an Object's Properties, Methods, and Events

After you assign an object reference to an object variable, you can use the variable to manipulate the object's properties and methods. You can also declare an object variable using the WithEvents keyword and use it to make your application respond to the object's events.

Using an Object's Properties and Methods

You can use the object.property syntax to set and return an object's property values or the object.method syntax to use methods on the object. For example, you could set the Caption property of the Application object as follows:

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Caption = "MyFirstObject"

Note   The Excel.Application syntax for referring to the Microsoft Excel Application class is not supported in versions prior to Microsoft Excel 97. To refer to the Microsoft Excel Application class in Microsoft Excel 5.0 and Microsoft Excel 95, use the syntax [_ExcelApplication] instead. For example:

Set xlApp = New [_ExcelApplication]

You could call the Quit method of the Microsoft Excel Application object like this:

xlApp.Quit

In general, it is a good idea to be as specific as possible when referring to methods or properties of objects defined by other applications or projects. For example:

' Fully qualified property name sets
' the Microsoft Project window caption.
Dim pjWindow As Project.Window
' Get a reference to the first Window object.
Set pjWindow = ActiveProject.Windows(1)
pjWindow.Caption = "Project Caption"

' Unqualified name causes Visual Basic to use
' the first object it finds with a property
' named Caption - in this case, Form1.
Caption = "Microsoft Form1 Caption"

Note   If you need to import binary data into your Visual Basic application and you plan to share the data between applications using ActiveX, use a Byte array to store the data. If you assign binary data to a string and then try to pass this data to an Automation object that takes a string, the data may not be converted correctly. "For more information on data types, see "Programming Fundamentals."

For More Information   "For more information on working with an object's properties and methods, see "Programming with Objects."

Responding to an Object's Events

In addition to responding to events that occur to Visual Basic objects, your application can respond to events in an object provided by an ActiveX component. For example, your Visual Basic application can display a message box if an event occurs in a Microsoft Excel workbook.

You make your application respond to an object's events by adding code to an event procedure for the object. However, event procedures for objects provided by components are not automatically available in Visual Basic. You must first declare an object variable using the WithEvents keyword.

After you declare an object variable using WithEvents, the Visual Basic code window uses the variable to display event procedures for the object. You can then add code to these event procedures to respond to the object's events. When you assign an object reference to the variable, you establish a connection between the variable and the object at run time.

To create an event procedure for an object provided by a component

  1. Add a reference to the component's type library to your Visual Basic project. For more information on adding a reference to a type library, see "Creating a Reference to an Object."

  2. In the Declarations section of a form or class module, declare an object variable using the WithEvents keyword. For example:

    Dim WithEvents xlBook As Excel.Workbook
    

    Visual Basic adds the name of the object variable to the Object box in the code window. When you select the variable name, Visual Basic displays the object's event procedures in the Procedure list box.

  3. Select an event procedure, then add code to the procedure that you want your application to run when the event occurs.

    For example, suppose your Visual Basic application relies on data displayed in a Microsoft Excel workbook and that you've already declared a WithEvents variable xlBook for the workbook. When a user tries to close the workbook, you can display a message and keep the workbook from closing by adding the following code to the xlBook_BeforeClose event procedure in your application:

    Private Sub xlBook_BeforeClose(Cancel As Boolean)
        ' Hide the Microsoft Excel window so the message
        ' will be visible.
        xlBook.Application.Visible = False
        ' Display the message.
        MsgBox "This workbook must remain open."
        ' Unhide the Microsoft Excel window.
        xlBook.Application.Visible=True
        ' Set the event procedure's Cancel argument
        ' to True, cancelling the event.
        Cancel = True
    End Sub
    
  4. Assign an object reference to the WithEvents object variable.

    For example, you could add the following to the Visual Basic form's Form_Load event procedure to assign the variable xlBook a reference to a Microsoft Excel workbook, Sales.xls:

    Private Sub Form_Load()
        Set xlBook = GetObject("Sales.xls")
        ' Display Microsoft Excel and the Worksheet
        ' window.
        xlBook.Application.Visible = True
        xlBook.Windows(1).Visible = True
    End Sub
    

For More Information   See "Dim Statement."