STATISTICA







Using STATISTICA Development Environment from within Visual Basic

Example 3: STATISTICA Scrollsheets can be saved as STATISTICA data files, and then accessed by other applications via the STATISTICA Development Environment. The example below shows how to access results stored within a Scrollsheet created in STATISTICA (and then saved as a STATISTICA data file) from another application (such as Visual Basic). Specifically, the program converts a STATISTICA Scrollsheet (saved as a data file) into a table saved in a ms Word '97 document. Using this method for accessing numeric results from within other applications along with the OLE method of accessing STATISTICA graphs, you can develop customized report templates. The report templates can contain STATISTICA graphs and numerical results that will be updated with the latest data.

If you would like to run this example, you may download CreateTable.zip. After unzipping this program (using WinZip or by renaming it as a self-extracting zip file, CreateTable.exe), you can run the setup program to install the CreateTable Visual Basic application. You can also open the Visual Basic project files if you have Visual Basic 5.0 loaded on your computer. Note that if you want to run this program from within Visual Basic, the Stadev32.dll must be placed in either the Windows System or the Visual Basic directory so that it is visible for Visual Basic run time. Note that this program will not work properly if Word '97 is not installed.


Visual Basic Code

Option Explicit
Private Sub cmdExit_Click()

    End 'Exit program

End Sub
Private Sub cmdAbout_Click()

    Dim Msg As String
    Dim Style As Integer
    Dim Title As String
    Dim Response As Integer
Msg = "Create Table" & Chr(13) & Chr(13) & "Release 1.0" & Chr(13) & Chr(13)_
& "This program will convert the specified STATISTICA data" & vbCrLf & _
"file into a Word table in the specified Word '97 document."
    Style = vbOKOnly
    Title = "About Create Table"
    Response = MsgBox(Msg, Style, Title) 				'Set About box text

End Sub
Private Sub cmdTab_Click()

    cmdAbout.Enabled = False
    cmdTab.Enabled = False
    cmdExit.Enabled = False

    Dim MyFile As String
    Dim MyData As Long
    With CommonDialog1
        .CancelError = True 						'Check if Open dialog canceled
        On Error GoTo Error1
        .Flags = cdlOFNFileMustExist 				'Set Open dialog flags
        .ShowOpen 							'Display Open dialog
    End With
    MyFile = CommonDialog1.filename 				'Assign user-specified filename
    MyData = StaOpenFile(MyFile)	'Open user-specified STATISTICA 'data file
    If MyData = 0 Then GoTo Error2 				'Check if file successfully opened


    Dim numvar As Integer
    Dim numcas As Long
    numvar = StaGetNVars(MyData) 				'Get number of columns in file
    numcas = StaGetNCases(MyData) 				'Get number of rows in file


    Dim MyDoc As Object
    Set MyDoc = CreateObject("Word.Document.8") 			'Create new Word object
    With MyDoc.Application
        .Visible = False 						'Hide Word application
        .Options.ReplaceSelection = False
    End With


    Dim MyString As String
    Dim vn(STAMAX_VARNAMELEN + 1) As Byte
    Dim cn(STAMAX_CASENAMELEN + 1) As Byte
    Dim d As Double
    Dim res As Integer
    Dim mdval As Double
    Dim MyCell As String
    Dim j As Integer
    Dim i As Long
    Dim lab(STAMAX_SLABELLEN + 1) As Byte
    Dim vwid As Integer
    Dim vdec As Integer
    Dim categ As Integer
    Dim dis As Integer
    Dim cformat As String
    Dim k As Integer

    For i = 0 To numcas 						'Start main loop for each row
    If i = 0 Then							'If row of column headings
    MyString = ";"
     For j = 1 To numvar
        res = StaGetVarName(MyData, j, vn(0)) 			'Get the column names
        If j = numvar Then MyString = MyString + BytesToString(vn) Else MyString =_
 MyString + BytesToString(vn) + ";"
     Next

    With MyDoc.Application
        .Selection.TypeText Text:=MyString 				'Type the column names
        .Selection.TypeParagraph
    End With

    MyString = ""

    Else 'If row of data
    res = StaGetCaseName(MyData, i, cn(0), 20) 			'Get row name
    MyString = BytesToString(cn) + ";"

     For j = 1 To numvar						'Start loop for each column
        res = StaGetData(MyData, j, i, d)				'Get row data
        res = StaGetVarMD(MyData, j, mdval) 			'Get missing data code

        If d = mdval Then 						'Check if missing
            MyCell = ""
        ElseIf 0 <> StaGetLabelForValue(MyData, j, d, lab(0)) Then 	'Check if text
            MyCell = BytesToString(lab)
        Else
            res = StaGetVarFormat(MyData, j, vwid, vdec, categ, dis) 	'Get column format

            Select Case categ
            Case 0 'Format is general number
                cformat = ""
                For k = 1 To vdec
                    cformat = cformat + "0"
                Next
								'Create general number format
                If vdec = 0 Then cformat = "#####" Else cformat = "#####." + cformat
            Case 1 							'Format is Date
                cformat = "Short Date"
            Case 2 							'Format is Time
                cformat = "General Date"
            Case 3 							'Format is special number
                If dis = 0 Or dis = 1 Then
                    cformat = "Scientific"
                ElseIf dis = 2 Or dis = 3 Then
                    cformat = "Currency"
                ElseIf dis = 4 Then
                    cformat = "Percent"
                End If
            End Select

            MyCell = Format(d, cformat) 				'Format data as specified above
        End If

        If j = numvar Then MyString = MyString + MyCell Else MyString =_
 MyString + MyCell + ";"					'Create row data string
     Next  							'Go to next column

    With MyDoc.Application
        .Selection.TypeText Text:=MyString 				'Type the row of data
        .Selection.TypeParagraph
    End With

    MyString = "" 							'Initialize data string variable

    End If
    Next  								'Go to next row

    Dim myrange As Object
Set myrange = MyDoc.Application.ActiveDocument.Range _
(Start:=MyDoc.Application.ActiveDocument.Paragraphs(1).Range.Start, _
End:=MyDoc.Application.ActiveDocument.Paragraphs(numcas + 1).Range.End)
    myrange.Select 						'Select all text


    Dim MyTable As Object
    Set MyTable = MyDoc.Application.Selection.ConvertToTable(Separator:=";", _
                  Format:=wdTableFormatClassic2, AutoFit:=True) 		'Convert text to a table

    With MyDoc.Application
        .Selection.Tables(1).Select 					'Select table
        .Selection.Copy 						'Copy table
    End With

    With CommonDialog2
        .CancelError = True 						'Check if Save dialog canceled
        On Error GoTo Error1
        .Flags = Val(cdlOFNOverwritePrompt) + Val(cdlOFNPathMustExist) 	'Set Save dialog flags
        .ShowSave							'Display Save dialog
    End With

    If OLE1.PasteOK = True Then OLE1.Paste Else GoTo Error3	'Paste table into view window

    With MyDoc.Application
        .ActiveDocument.SaveAs filename:=CommonDialog2.filename 	'Save document as user-specified
'filename
        .Quit SaveChanges:=wdDoNotSaveChanges 			'Close Word application
    End With

    cmdAbout.Enabled = True
    cmdTab.Enabled = True
    cmdExit.Enabled = True

    StaCloseFile (MyData) 						'Close STATISTICA data file
    Exit Sub

Error1:
    cmdAbout.Enabled = True
    cmdTab.Enabled = True
    cmdExit.Enabled = True
    Exit Sub
Error2:
    cmdAbout.Enabled = True
    cmdTab.Enabled = True
    cmdExit.Enabled = True
    MsgBox "This STATISTICA file could not be opened. It may be in use by another application.  Close the application and try again."
    Exit Sub
Error3:
    cmdAbout.Enabled = True
    cmdTab.Enabled = True
    cmdExit.Enabled = True
    MsgBox "The table cannot be viewed."
    Exit Sub

End Sub
Private Function BytesToString(byte_array() As Byte) As String

    Dim Data As String, StrLen As String
    Data = StrConv(byte_array(), vbUnicode)
    StrLen = InStr(Data, Chr(0)) - 1
    BytesToString = Left(Data, StrLen)

End Function
Private Sub mnuABt_Click()

    cmdAbout_Click

End Sub
Private Sub mnuExit_Click()

    cmdExit_Click

End Sub
Private Sub mnuTab_Click()

    cmdTab_Click

End Sub
Back to List of Programs


[StatSoft]
2300 East 14th Street, Tulsa, OK 74104
Phone: (918) 749-1119; Fax: (918) 749-2217

[StatSoft]e-mail: info@statsoft.com

©Copyright StatSoft, Inc., 1984-2005.
StatSoft, StatSoft logo, STATISTICA, SEWSS, SEDAS, Data Miner, SEPATH and GTrees are trademarks of StatSoft, Inc.