Converting Visual Basic ActiveX Examples to Equivalent LabVIEW

Contents

Introduction

Many ActiveX examples have been written in a variety of programming languages to demonstrate the functionality of various ActiveX components. These existing examples, while extremely useful, are sometimes difficult to translate into corresponding LabVIEW examples.

Regardless of the development language you use, you must complete the following steps to use objects provided by ActiveX components:

  1. Create a reference to the object you want to use. How you create the reference depends on the type of object and if the ActiveX component supplies a type library.
  2. Write code using the properties, methods, and events for the object.
  3. Release the object when you are finished using it.


This document explains the correspondence between Visual Basic ActiveX calls and LabVIEW. For more information about using ActiveX in LabVIEW, refer to ActiveX and LabVIEW (linked below).

See Also:
ActiveX and LabVIEW

Creating an Object Reference


Before you can use the properties, methods, and events for an ActiveX object in an application, you must open an object reference to an automation server. How you assign an object reference depends on the following factors:

  • The ActiveX component supplies a type library--The type library contains definitions of all the objects the component provides, including definitions for all available properties, methods, and events.
  • Whether the object is a top-level, externally creatable object, or a dependent object--You can assign a reference to an externally created object directly, while references to dependent objects are assigned indirectly. An example of an externally creatable object would be the Application object for Excel. An example of a dependent object is the Workbooks object, which is dependent on the creation of an Application object.


If an object is externally creatable, you can assign an object reference to a variable in Visual Basic by using the New keyword, CreateObject, or GetObject in a Set statement from outside the component. If the object is a dependent object, you assign an object reference by using a method of a higher-level object in a Set statement.

In LabVIEW you use the Automation Open function to create the reference to an externally creatable object. To assign a dependent object reference, use a method of a higher-level object just as you would in Visual Basic.

Note: To view externally creatable objects in LabVIEW, right-click the Automation Open function, select Select ActiveX Class»Browse from the shortcut menu to display the Select Object From Type Library dialog box, and place a checkmark in the Show Creatable Objects Only checkbox. Otherwise, the dialog box also displays all dependent objects as well.

The following example demonstrates opening externally creatable reference to Excel using Visual Basic and LabVIEW.

Visual Basic
LabVIEW
Dim XlApp As Excel.Application
Set XlApp = New Excel.Application

Or

Dim XlApp
Set XlApp = CreateObject("Excel.Application")

 
The GetObject keyword is used in the same manner as CreateObject, except if a reference already exists GetObject returns the existing reference. By default, LabVIEW returns a existing reference. If a reference does not exist, LabVIEW opens a new one. To open a new reference automatically, set the open new instance input of the Automation Open function to TRUE, as is shown in the previous example.

In comparison, the following example demonstrates using a dependent object reference in Visual Basic and LabVIEW. A LabVIEW VirtualInstrument is a dependent object of the Application reference. You must create the Application object first and then create a VirtualInstrument from that object.

Visual Basic
LabVIEW
Dim MyApp As LabVIEW.Application
Dim MyVI As LabVIEW.VirtualInstrument Set MyApp = New LabVIEW.Application
Set MyVI = MyApp.GetVIReference(Path)


After you have an open reference to the object, you can use its available properties and methods.

Using Object Properties and Methods


After you create an object reference, you can use the reference to manipulate the properties and methods of the object. You also can make your application respond to object events.

In Visual Basic you use the object.property syntax to set and return property values or the object.method syntax to use methods on the object. The following example sets the Caption property of the Excel Application object:

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

To create the preceding example in LabVIEW, wire an Application reference to a Property Node, right-click the Property Node, select Select Property from the shortcut menu, and select the Caption property from the list of available properties for the object.

After you select the Caption property, wire a string constant containing the caption text to the input of the Caption property, as shown in the following example:


To use an object method, wire an Application reference to an Invoke Node, right-click the Invoke Node, select Select Method from the shortcut menu, and select a method from the list of available methods for the object. The following example uses the Save method for the Application object:

Releasing an Object


When you finish using an object, you should clear any references to the object to release the object from memory and prevent a memory leak.

In Visual Basic, you set an object variable to Nothing to release the object from memory. The following example sets the acApp object to Nothing, thus disposing of the reference to the object and removing it from memory:

Dim acApp As Access.Application
Set acApp = New Access.Application
MsgBox acApp.SysCmd(acSysCmdAccessVer)
Set acApp = Nothing

Be careful to set all object references to Nothing when finished, even for dependent objects, as shown in the following example:

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlApp = Nothing
Set xlBook = Nothing 'Now all the references are cleared.

You must also dispose of object references In LabVIEW. Use the Close Reference function to dispose of object references. If multiple references are open, close them when you're finished using them. Refer to the Converting an Example for an example of disposing of object references.

Navigating Object Models


After you understand how to use objects provided by components, you can use any object that a component exposes to you. Components can range from a simple code component or ActiveX control to large components, such as LabVIEW or Microsoft Excel, which expose many objects.

Each object exists in the object hierarchy of the component, and you can access the objects in two ways:

  • Directly if the object is externally creatable
  • Indirectly if the object is a dependent object--You can get a reference to it from another object higher in the hierarchy.

In LabVIEW, use the Select Object From Type Library dialog box to navigate an object hierarchy. Right-click the Automation Open function and select Select ActiveX Class»Browse from the shortcut menu to display this dialog box. The dialog box displays all objects available in each component, as shown in the following figure.



The dialog box displays creatable objects only when you place a checkmark in the Show Creatable Objects Only checkbox. Creatable objects are designated by a blue dot preceding their names, as shown in the following figure.

Navigating the Object Hierarchy


Some objects have very simple hierarchies. For example, LabVIEW has the externally creatable Application object and the dependent VirtualInstrument object, as shown in the following figure.



By comparison, Excel has a complex hierarchy composed of over 100 objects. The following figure shows a small segment of that hierarchy.


For more information about component object hierarchies, refer to the documentation for that component. For example, LabVIEW and Microsoft Excel include component hierarchies and descriptions of objects and their associated properties, methods, and events.

Converting an Example


The following example converts a Visual Basic example that uses the Excel automation server into a LabVIEW application. This example includes externally creatable objects and indirect dependent objects and uses object methods and properties. The application takes two values entered into the automation client (Visual Basic or LabVIEW), passes them to cells in Excel, creates a formula to add them in Excel, and then saves the worksheet to disk.

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


The following block diagram implements the same application as a LabVIEW VI:

Was this information helpful?

Yes

No