Introduction to objects
The real power of mrScriptBasic is that it enables scriptable access to the UNICOM Intelligence components. It does this through its ability to access the UNICOM Intelligence objects. This topic provides a brief introduction to objects for those who are new to them.
This topic uses Microsoft Excel to illustrate how to work with objects. Excel consists of an application you might be familiar with and an underlying set of objects that do most of the work.
Recording a macro
You start by recording a macro and then examining the Visual Basic code that is automatically generated.
To record the macro
1 Start Excel.
2 From the menus, choose Tools > Macro > Record New Macro.
This opens the Record Macro dialog box.
3 Accept the defaults and click OK.
This returns you to the main Excel window where you will see a little floating Macro toolbar. Ignore this for the moment.
4 Enter 1 in cell A2, 2 in cell A3, and 3 in cell A4.
5 Click the Stop Recording tool on the Macro toolbar.
To see the macro's Visual Basic code
1 From the menus, choose Tools > Macro > Macros.
This opens the Macro dialog box.
2 Click Edit.
This opens the Visual Basic Code window. It contains code like this:
Sub Macro1()
'
' Macro1 Macro
'
Range("A2").Select
ActiveCell.FormulaR1C1 = "1"
Range("A3").Select
ActiveCell.FormulaR1C1 = "2"
Range("A4").Select
ActiveCell.FormulaR1C1 = "3"
End Sub
The code echoes what you entered in Excel. Line 1 selects cell A2. Line 2 puts “1” in the selected cell. Line 3 selects cell A3. Line 4 puts “2” in the selected cell. Line 5 selects cell A4. Line 6 puts “3” in the selected cell.
The macro demonstrates the Excel Range and ActiveCell objects. In the first line you are essentially telling Excel's Range object to select cell A2:
Range("A2").Select
This line means “Tell the Range object to select cell A2”. It uses the Range object's Select method. Objects have inbuilt methods and properties. The methods are like mini-programs that are built into the object to do something. For example, the Select method makes the first cell defined by the Range object the “active” cell. Properties, on the other hand, typically define attributes of the object, things like its name or color, for example.
In the above example, FormulaR1C1 is a property of the ActiveCell object. The ActiveCell object is actually contained within a property of the Excel Application object. However, because this is a macro, the Excel Application object isn't specified, because the macro is designed for running within the application.
You aren't limited to using Excel objects in Excel macros. You can use them from almost any program or script. We can easily demonstrate this using Visual Basic Scripting Edition (VBScript), which is automatically installed with Internet Explorer.
Set up and run a VBScript file
Next, set up and run a VBScript file to do the same thing as the macro.
1 Copy or type the following code into a text file and saving it with the name Excel.vbs. Notice that this time we need to create and specify the Excel Application object and add a Workbook object:
Dim Excel
Set Excel = CreateObject("Excel.Application")
Excel.Workbooks.Add
Excel.Range("A2").Select
Excel.ActiveCell.FormulaR1C1 = "1"
Excel.Range("A3").Select
Excel.ActiveCell.FormulaR1C1 = "2"
Excel.Range("A4").Select
Excel.ActiveCell.FormulaR1C1 = "3"
Excel.Visible = True
Set Excel = Nothing
2 Run the script file. If you're still running Excel from the previous exercise, close it, otherwise you'll miss out on the full effect. To run Excel.vbs, browse to its location in Windows Explorer and double-click it.
The file opens Excel, and then puts 1, 2, and 3 in the cells A2, A3, and A4, respectively. The script is a file on your computer and it uses the Excel objects, but it does not require Excel to be running already (although it does require Excel to be installed). That is, the script is not an Excel macro, but exists standalone, outside of Excel.
There are actually a lot of Excel objects. As you can see from the above example, there is an object that represents the application and an object that represents a workbook. There's also an object that represents a worksheet, another one that represents a cell, and so on. In fact, Excel consists of about 183 objects altogether.
One reason there are so many objects is that objects often contain other objects. For example, one way to put “42” in a cell is to write code like this:
Application.Workbook.Worksheet.Cell = 42
The . (dots) reference each object in turn. So, reading the code right-to-left, this line says “put 42 in the Cell object that is in the Worksheet object that is in the Workbook object that is in the Application object”.
Finding out about objects
Visual Basic has a feature called the Object Browser for finding out about the objects you are using and their relationships to each other.
1 Go back to the Visual Basic Code window in Excel. (A quick way of doing this is to go into Excel and press ALT + F11). Then select Object Browser from the View menu (or press F2). This opens the Visual Basic Object Browser.
2 Select Excel from the list in the top left corner.
All of the Excel objects are listed on the left side of the browser. The right side lists the properties and methods of the object you select on the left. The lower part of the window displays details of the property or method that you select on the right side.
For example, if you select the Application object on the left side, and then select the ActiveCell property on the right side, you will see details of the ActiveCell property in the lower part of the window. This shows that the ActiveCell property is an object of type Range. If you click Range in the lower part of the window, you jump to the Range object and you can see all of its properties and methods.
Objects, applications and scripts
Software revolves around objects, applications, and scripts. Excel has two parts--an application and a set of objects (which is sometimes referred to as the object model). The application is the user interface where you point, click, and type. The set of objects does the work. The application appears to be doing the work but in fact it delegates the work to the Excel objects.
The fact that Excel is separated into an application and objects is what makes it possible to write macros and scripts that can take advantage of Excel's functionality in new ways. When you want to use the functionality of Excel in a way the Excel application doesn't support, you can write a macro or a script (or even your own application) that uses the Excel objects in new ways.
The principles of using objects in mrScriptBasic are the same as in the above examples. mrScriptBasic is very similar to VBScript but has been designed specifically to meet the needs of the market research industry. For example, mrScriptBasic provides built-in support for market research data types.
With just a little work, we can change our VBSscript code to turn it into valid mrScriptBasic code. The changes you need to make to the code are:
1 Enclose the parameters to all properties in [] (brackets) and not () (parentheses) as in VBScript. For example, in the following line, ("A2") is a parameter to the Range property of the Excel Application object, and it means that the Range we want to select is actually the A2 cell.
Excel.Range("A2").Select
In mrScriptBasic, you change the parentheses to brackets so that it looks like this:
Excel.Range["A2"].Select
2 Follow methods by () (parentheses). For example, in Visual Basic you do not generally use parentheses when you call a method or subroutine that does not return a value. That's why the call to the Select method is specified like this:
Excel.Range.Select
However, in mrScriptBasic when you call a method or a subroutine, you must always use parentheses, like this:
Excel.Range.Select()
3 Replace the Nothing keyword with the Null keyword. The Nothing keyword is a Visual Basic keyword that is not valid in mrScriptBasic. However, in mrScriptBasic you can use the Null keyword instead.
Here is the code after the changes have been made and with the changes highlighted:
Dim Excel
Set Excel = CreateObject("Excel.Application")
Excel.Workbooks.Add()Excel.Range["A2"].Select()Excel.ActiveCell.FormulaR1C1 = "1"
Excel.Range["A3"].Select()Excel.ActiveCell.FormulaR1C1 = "2"
Excel.Range["A4"].Select()Excel.ActiveCell.FormulaR1C1 = "3"
Excel.Visible = True
Set Excel = Null
4 Change the name of the file so that it has an .mrs filename extension (for example, Excel.mrs).
Running the code
1 Open a Command Prompt. For example, on Windows 2000, from the Windows Start menu, choose: Programs > Accessories > Command Prompt
2 Change to the [INSTALL_FOLDER]\IBM\SPSS\DataCollection\<version>\Accessories folder.
3 Type mrScriptCL followed by the path and name of the file and then press Enter. For example:
mrScriptCL C:\Samples\Excel.mrs
Just like Excel.vbs, Excel.mrs Excel opens Excel and puts the values 1, 2, and 3 in the cells A2, A3, and A4, respectively.
Using UNICOM Intelligence and UNICOM Intelligence Data Model objects
So now you have seen for yourself that you can use mrScriptBasic with the Excel objects. Just like VBScript, you can also use mrScriptBasic with any other objects that are registered on your computer. However, mrScriptBasic has been designed especially to make it easy to use with the UNICOM Intelligence and UNICOM Intelligence Data Model objects.
The main sets of objects in the UNICOM Intelligence Data Model are called the UNICOM Intelligence Case Data Model and the Metadata Model. The Case Data Model objects allow both applications and scripts to easily access response data, which stores the answers that respondents give to questionnaires. The Metadata Model (MDM) objects allow both applications and scripts to easily access survey metadata, which stores all of the information about the questionnaire used to collect the response data, including question and category names and labels, and sometimes the survey routing logic.
You can use the Visual Basic Object Browser to browse the UNICOM Intelligence and UNICOM Intelligence Data Model objects, just like we used the Object Browser to browse the Excel objects. For example, suppose you want to browse the MDM objects:
1 Go back to the Excel Visual Basic code window.
2 From the Tools menu, choose References.
3 Scroll to SPSS MR MDM 2.0 Type Library, and then select it.
4 Click OK.
5 Press F2 to open the Object Browser, and then select MDMLib from the list in the top left corner.
You will now be able to browse the MDM objects just like we browsed the Excel objects earlier.
More information
mrScriptBasic examples includes examples of using mrScriptBasic with UNICOM Intelligence Data Model and UNICOM Intelligence objects.
The UNICOM Intelligence Developer Documentation Library provides detailed documentation of the properties and methods of most of the UNICOM Intelligence objects that you are likely to want to use. Generally, documentation of the objects is in the section of the UNICOM Intelligence Developer Documentation Library that relates to the product to which the objects belong. For example, documentation of the UNICOM Intelligence Data Model objects is in
UNICOM Intelligence Data Model reference, and documentation of Data Management objects is in
Data Management reference. For each set of objects, an object model diagram is also provided; it shows how the objects relate to each other.
UNICOM Intelligence Professional includes a complete set of tools (including a built-in object browser similar to the Visual Basic one) to help you create mrScriptBasic scripts. See
Creating your first mrScriptBasic script for more information.
See also