Macro (SVB) Programs Example - Opening and Saving Statistica Documents

Several examples, in particular Recording Simple Macros, Documenting an Analysis, illustrate how to open and close spreadsheets for data input and how to "connect" them to an analysis. To reiterate, there are two ways in which data files can be opened and used in an analysis. First, you can open the data spreadsheet explicitly, and then activate it (i.e., bring it to the top so it will be the active spreadsheet for the subsequent analyses), or you can open a spreadsheet by passing the respective file name when creating a new analysis. So the following two sets of statements will have the same effect:

Dim InputFile As Spreadsheet 
' Open the Spreadsheet (data) file and assign 
' the Spreadsheet object to variable InputFile. 
Set InputFile=Spreadsheets.Open( _ Path & "\Examples\Datasets\exp.sta")
		
' Set the .Visible property to True (make 
' Spreadsheet visible) 
InputFile.Visible=True 
InputFile.Activate 
Set newanalysis = Analysis (scBasicStatistics)
		
Or, alternatively, simply:
Set newanalysis = Analysis (scBasicStatistics, _ 
Path & "\Examples\Datasets\exp.sta" )

In general, the spreadsheet object has many properties and methods that allow you to operate on those objects, i.e., to edit them, copy, paste, or save them. To review all properties and methods available for the spreadsheet, display the Object Browser (ribbon bar: select the View tab. In the Browser group, click Object. classic menus: from the View menu, select Object Browser or click the Object Browser toolbar button), select the Statistica library, and highlight the Spreadsheet object in the left panel; the right panel will then display the available properties and methods.

Most methods and properties are self-explanatory.

Saving Spreadsheets
To save a spreadsheet, you could use the SaveAs method:
' This program will save a Spreadsheet with 
' descriptive statistics. 
Sub Main 
' Create the Basic Statistics analysis object.

Set newanalysis = Analysis (scBasicStatistics, _ Path & "\Examples\DataSets\Exp.sta")

' Here we are "running" the Basic Statistics; 
' the next four lines were recorded as a macro, 
' and then modified for this example.

newanalysis.Dialog.Statistics = scBasDescriptives 
newanalysis.Run newanalysis.Dialog.Variables = "5 6 7 8" 
Set ResSpreadsheet=newanalysis.Dialog.Summary

'Save the results Spreadsheet as ...\Descriptives.sta.

If (ResSpreadsheet.Item(1).SaveAs( _ Path & "\Examples\DataSets\Descriptives.sta")) _ Then
MsgBox "Saved"
Else
MsgBox "Not Saved"
End If

' Now save it again, this time with the optional second 
' parameter; if set to True, then any existing file will 
' by the same name will automatically be overwritten.

If (ResSpreadsheet.Item(1).SaveAs( _ Path & "\Examples\DataSets\Descriptives.sta", _ True)) Then
MsgBox "Saved"
Else
MsgBox "Not Saved"
End If

End Sub
Note: the Summary results spreadsheet always returns a collection of spreadsheet objects, as described in The Statistica VB Object Model. Hence, the .SaveAs method is applied to .Item(1) of the Summary results collection (which, in this case, is the only item produced). The program will save the Summary results spreadsheet twice under the same name. The first time, if a file by that name (in that directory) already exists, the .SaveAs method will return False, and the new spreadsheet will not be saved (i.e., not overwrite the pre-existing file). The second time, the .SaveAs method is called with a second (optional) parameter set to True, and this time, the program will overwrite any existing files by the same name.
Saving graphs
Graph objects also have many properties and methods; one of those is .SaveAs. Shown below is an example program that will save a series of histograms created via the Basic Statistics Descriptive Statistics options.
' This program will save histograms created via 
' the Basic Statistics Descriptive Statistics options. 
Sub Main

Dim i As Integer 
Dim GName As String

' Create the Basic Statistics analysis object
Set newanalysis = Analysis (scBasicStatistics, _ Path & "\Examples\DataSets\Exp.sta")

' Here we are "running" the Basic Statistics; ' the next four lines were recorded as a macro, ' and then modified for this example.
newanalysis.Dialog.Statistics = scBasDescriptives newanalysis.Run newanalysis.Dialog.Variables = "5 6 7 8" Set ResGraphs=newanalysis.Dialog.Histograms

'Save the results histograms as ...\Histo 1.sta, Histo 2.stg, etc.
For i=1 To ResGraphs.Count

GName = Path & "\Examples\DataSets\Histo"+Str(i)+".stg" ResGraphs.Item(i).SaveAs( GName)

Next i
End Sub
Note: in this case the program cycles through all .Count objects in the collection of graphs in ResGraphs, and all histograms are saved in consecutive files.
Opening spreadsheets and graphs
After you ran the two example programs shown above that created files with descriptive statistics, and graphs, you can open and display those files using the .Open method. Shown below is a program that will open the files that were created and move them into a new workbook.
Dim G As Graph Dim Sname As String 
Dim GName As String Dim i As Integer

' Create a new Workbook.
Set ResultsWorkbook = Workbooks.New ResultsWorkbook.Visible=True

' This file was previously saved and must ' already exist in this location.
Sname="C:\Program Files\Statistica\Statistica *\Examples\DataSets\Descriptives.sta"

' Open the Spreadsheet
Set SS=Spreadsheets.Open(Sname)

' Place it into the Workbook
ResultsWorkbook.InsertObject(SS,ResultsWorkbook.Root, ) For i=1 To 4

' This file was previously saved, and must ' already exist in this location
GName="C:\Program Files\Statistica\Statistica *\Examples\DataSets\Histo"+Str(i)+".stg"

' Open the previously saved graph
Set G=Graphs.Open(GName)

' Place it in a workbook
ResultsWorkbook.InsertObject(G,ResultsWorkbook.Root, )

Next i
End Sub

For details concerning workbooks and how to manage the contents of workbooks via SVB, see also Customizing Workbooks.