Share via


Visual Basic Concepts

Working with ActiveX Components

You work with object provided by ActiveX components in much the same way that you work with other objects. You assign an object reference to a variable, then write code that uses the object's methods, properties, and events. However, there are some things you need to be aware of when you work with objects provided by components.

This topic provides an overview of the top-level tasks for working with objects provided by components and an example of using objects in an ActiveX-enabled application. For details on each task, see the appropriate topic described under each task item.

To use most objects provided by ActiveX components

  1. Create a reference to the object you want to use. How you do this depends on the type of object and whether the ActiveX component supplies a type library.

    For more information   See "Creating a Reference to an Object" later in this chapter.

  2. Write code using the object's methods, properties, and events.

    For more information   See "Using an Object's Properties, Methods, and Events" later in this chapter.

  3. Release the object when you are finished using it.

    For more information   See "Releasing an ActiveX Component" later in this chapter.

  4. Create error-handlers.

    For more information   See "Handling Run-Time Errors in ActiveX Components" later in this chapter.

For example, suppose you have created a form with three text boxes (Text1, Text2, and Text3) and a command button (Command1), and added a reference in your project to the Microsoft Excel 8.0 Object Library. You can then add code to the command button's Command1_Click event procedure that uses the Microsoft Excel Formula method to add two numbers entered in Text1 and Text2, displaying the result in Text3. (To avoid a type mismatch error, you may want to remove the default text value of each text box by setting its Text property to an empty string):

Private Sub Command1_Click()
   ' Declare object variables for Microsoft Excel,
   ' application workbook, and worksheet objects.
   Dim xlApp As Excel.Application
   Dim xlBook As Excel.Workbook
   Dim xlSheet As Excel.Worksheet

   ' Assign object references to the variables. Use
   ' Add methods to create new workbook and worksheet
   ' objects.
   Set xlApp = New Excel.Application
   Set xlBook = xlApp.Workbooks.Add
   Set xlSheet = xlBook.Worksheets.Add

   ' Assign the values entered in the text boxes to
   ' Microsoft Excel cells.
   xlSheet.Cells(1, 1).Value = Text1.Text
   xlSheet.Cells(2, 1).Value = Text2.Text

   ' Use the Formula method to add the values in
   ' Microsoft Excel.
   xlSheet.Cells(3, 1).Formula = "=R1C1 + R2C1"
   Text3.Text = xlSheet.Cells(3, 1)

   ' Save the Worksheet.
   xlSheet.SaveAs "c:\Temp.xls"

   ' Close the Workbook
   xlBook.Close
   ' Close Microsoft Excel with the Quit method.
   xlApp.Quit

   ' Release the objects.
   Set xlApp = Nothing
   Set xlBook = Nothing
   Set xlSheet = Nothing
End Sub

For simplicity, this example doesn't include error handling. However, it is highly recommended that you include error handling in applications that use objects provided by ActiveX components.