Objects, Methods, and Properties

What makes the Visual Basic environment so powerful is the ability to integrate and manipulate various applications and their environments into a single program. For example, you can write a Visual Basic program (SVB program) that will compute basic descriptive statistics via Statistica Descriptive Statistics and transfer that matrix into an Excel spreadsheet or a Microsoft Word document.

The exchange of information between different applications is accomplished by exposing those applications to the Visual Basic programs as objects. So, for example, you can run statistical analyses in the Statistica Basic Statistics module from a Visual Basic program in Excel by declaring inside the program an object of type Statistica.Application.

Once an object has been created, the Visual Basic program then has access to the properties and methods contained in that object. Properties can be mostly thought of as variables, methods can be mostly thought of as subroutines or functions that perform certain operations or computations inside the respective application object.

To illustrate, we will create an example program that can be run from Visual Basic within Excel.

Note: Excel 2007 and earlier versions are 32-bit; the Statistica libraries can be accessed from Excel only if the 32-bit version of Statistica is installed.

After starting Excel, create a new worksheet. From the Tools menu, select Macro - Visual Basic Editor.

Note for Excel 2007 and 2010: To access Visual Basic, after starting Excel press ALT+F11 or:

In Excel 2007, click the Office button in the upper-left corner of the application, and click the Excel Options button. In the Excel Options dialog box, select Popular. Select the Show Developer tab in the Ribbon check box, and click the OK button. On the Excel ribbon bar, select the Developer tab. In the Code group, click Visual Basic.

In Excel 2010, select the File tab. Click Options. In the Excel Options dialog box, select Customize Ribbon. In the Customize the Ribbon: Main Tabs pane, select the Developer check box, and then click the OK button. On the Excel ribbon bar, select the Developer tab. In the Code group, click Visual Basic.

From the Visual Basic Tools menu, select References.

From the References dialog, you can select the libraries (objects) that you would like to be visible inside the Visual Basic program. To make Statistica visible, select the Statistica Object Library, and the Statistica Basic Statistics Library (for the current version of Statistica); then click OK.

Now type or paste in the following program into the program editor:

Sub ExcelTest()

' Run the STATISTICA application; create the STATISTICA

' Application object and assign it to variable (object) x.

' Note that in order to assign objects to variables, you need

' to use the syntax: Set Variable = Object.

Set x = CreateObject("Statistica.Application")

' Create a STATISTICA Basic Statistics object (i.e., run the

' Basic Statistics module; start it with data file exp

' (note: the actual location of that data file may be

' different on your installation).

Set a = x.Analysis(scBasicStatistics, _
& "\Examples\Datasets\exp.sta")

' the following 7 lines of code will produce a summary results

' Spreadsheet from the STATISTICA Basic Statistics module.

a.Dialog.Statistics = scBasDescriptives


a.Dialog.Variables = "5-8"

Set out = a.Dialog.Summary

' Select all rows and columns in the STATISTICA results Spreadsheet.


' Copy the highlight selection (all rows and columns in the

' Summary results Spreadsheet.


' Set the cursor to cell A1 in the currently active Excel Spreadsheet.


' Paste in the summary statistics.

ActiveSheet.PasteSpecial Format:="Biff4"

End Sub

When you run this Visual Basic program from inside Microsoft Excel (Visual Basic Editor), it will paste the results from the Summary results spreadsheet of the Basic Statistics - Descriptive Statistics analysis into the current Excel Spreadsheet. Note that this is accomplished without the user ever seeing or having to interact with the Statistica application; the program runs entirely invisibly, and the results simply appear inside the Excel spreadsheet. This simple example illustrates the power and versatility of the Statistica Visual Basic Object Model. All analysis and graphics options and methods available in Statistica are fully exposed in the respective object libraries, and even advanced and complex analyses can be automated and performed routinely "behind the scenes" from within any other Visual Basic compatible application.