Introduction to objects
mrScriptBasic enables scriptable access to the UNICOM Intelligence components. It does this through its ability to access the UNICOM Intelligence objects. This topic provides an introduction to objects.
This topic uses Microsoft Excel to illustrate how to work with objects.
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 Microsoft Excel.
2 Click Tools > Macro > Record New Macro.
The Record Macro dialog opens.
3 Accept the defaults, and then click OK.
This returns you to the main Excel window, which now contains a 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 toolbar button.
To see the macro's Visual Basic code
1 Click Tools > Macro > Macros.
The Macro dialog opens.
2 Click Edit.
The Visual Basic Code window opens. 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. The first line tells Excel's Range object to select cell A2:
Range("A2").Select
It uses the Range object's Select method. Objects have inbuilt methods and properties:
▪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 typically define attributes of the object, things like its name or color, for example.
In the example, FormulaR1C1 is a property of the ActiveCell object. The ActiveCell object is actually contained in a property of the Excel Application object. However, because this is a macro, the Excel Application object is not specified, because the macro is designed for running in the application.
You are not limited to using Excel objects in Excel macros. You can use them from almost any program or script. This can be demonstrated by using Visual Basic Scripting Edition (VBScript).
Set up and run a VBScript file
Next, set up and run a VBScript file to do the same action as the macro.
1 Copy or type the following code into a text file and saving it with the name Excel.vbs. This time, you must 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 Close Microsoft Excel.
3 To run Excel.vbs, go to its location in Windows Explorer, and then 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. That is, the script is not an Excel macro, but exists standalone, outside of Excel.
There are a lot of Excel objects that represent the application, a workbook, a worksheet, a cell, and so on. 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 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 includes an 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: in Excel, press ALT + F11.
2 Click View > Object Browser, or press F2.
The Visual Basic Object Browser opens.
3 Select Excel from the list.
The browser shows a list of the Excel objects. When you click an object, the browser displays its properties and methods, and then the details of the property or method that you click.
For example, if you click the Application object, and then click the ActiveCell property on the right side, the browser displays the details of the ActiveCell property. The details show that the ActiveCell property is an object of type Range. If you click Range, you go to the Range object, and you can see its properties and methods.
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 does not 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 preceding examples. mrScriptBasic is 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.
You can change the VBSscript code into mrScriptBasic code. The changes you need to make 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 you want to select is actually the A2 cell.
Excel.Range("A2").Select
In mrScriptBasic, 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; for example, 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.
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 its extension is .mrs (for example, Excel.mrs).
Running the code
1 Open a command prompt, and then go to the [INSTALL_FOLDER]\IBM\SPSS\DataCollection\<version>\Accessories folder.
2 Type mrScriptCL followed by the path and name of the file, and then press Enter. For example:
mrScriptCL C:\Samples\Excel.mrs
Excel.mrs opens Excel, and then puts the values 1, 2, and 3 in the cells A2, A3, and A4.
Using UNICOM Intelligence and UNICOM Intelligence Data Model objects
You can also use mrScriptBasic with any other objects that are registered on your computer. However, mrScriptBasic is designed to be used 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 look at the UNICOM Intelligence and UNICOM Intelligence Data Model objects, just like you used to look at Excel objects. For example, suppose you want to browse the MDM objects:
1 Go back to the Excel Visual Basic code window.
2 Click Tools > References.
3 Click SPSS MR MDM 2.0 Type Library, and then click OK.
4 Press F2 to open the Object Browser, and then select MDMLib from the list.
You can now browse the MDM objects.
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 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 to create mrScriptBasic scripts. For more information, see
Creating your first mrScriptBasic script.
See also