1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
Sub Main
'Begin Description
'When developing an Autoscript, it is often tedious to re-generate the table
'which triggers it.  Use this as a testbed for your autoscript code.  Just 
'select a table which you want to write an autoscript for.  Add your code
'between the rows marked by asterisks below.  Run the script as often as you
'need to for testing.  When done debugging, use Create/Edit autoscript,
'cut all your code from between the two rows of asterisks, and paste it
'into your autoscript subroutine.
'End Description

	'THERE SHOULD BE NO NEED TO MODIFY THIS PORTION OF THE SCRIPT
	'these are the three variables an autoscript is called with
	Dim objPivotTable As Object
	Dim objOutputDoc As Object
	Dim lngIndex As Long
	
	Dim objItem As ISpssItem	'this is just so we can de-activate the table
	
	Dim bolFoundPivot As Boolean
	
	If Not GetFirstSelectedPivotForAutoscript(objPivotTable, objOutputDoc, lngIndex, objItem) Then
		Exit Sub
	End If
	
	'**********************************************************************
	'Everything will behave as it would if an autoscript had been triggered:
	'Sub EXPERIMENT(objPivotTable As Object, objOutputDoc As Object, lngIndex As Long)
	'Clip everything below the next line and paste into your autoscript.
	'**********************************************************************
	
	'trivial example for purposes of illustration only.
	Dim strReport As String
	
	strReport = "Item found is index: " & lngIndex & vbCrLf  
	strReport = strReport & "Table Title: " & objPivotTable.TitleText
	
	MsgBox strReport, vbInformation, "Autoscript Experiment"
	
	'**********************************************************************
	'Clip everything above the previous line and paste into your autoscript.
	'**********************************************************************
	objItem.Deactivate
End Sub


Function GetFirstSelectedPivotForAutoscript(objPivotTable As Object, objOutputDoc As Object, lngIndex As Long, objItem As ISpssItem) As Boolean
'Purpose: Find the first selected Pivot Table and return values as if an 
'		  autoscript had been triggered
'Assumptions: A Pivot Table is selected in the Output Doc (Navigator)
'Effects: Activates the selected Pivot Table
'Inputs: PivotTable object, Item object that contains selected PivotTable
'Return Values: Selected PivotTable, Output Document, Index of Item, 
'		Item In the Navigator
'		True if successful

	Dim objDocuments As ISpssDocuments     	' SPSS documents.
	Dim objItems As ISpssItems       		' Output Navigator items 
	Dim intItemCount As Integer
	Dim intItemType As Integer
	Dim bolSelected As Boolean             	' True if an item is selected.
	Dim i As Integer
	
	'Get list of documents in SPSS.
	Set objDocuments = objSpssApp.Documents
	
	' Get designated document only if there is at least one output document.
	If objDocuments.OutputDocCount > 0 Then
	   'Get the currently designated output document.
	   Set objOutputDoc = objSpssApp.GetDesignatedOutputDoc
	Else
		'If no navigator window exists.
		MsgBox( "No navigator document found." )
		GetFirstSelectedPivotForAutoscript = False
		Exit Function
	End If
	
	' We have not found a pivot table.(Set flag)
	bolFoundPivot = False

	' Get the outline tree and the number of items:
	Set objItems = objOutputDoc.Items
	intItemCount = objItems.Count

	' Get the first selected pivot table.
	For i = 0 To intItemCount - 1
		Set objItem = objItems.GetItem(i)
		intItemType = objItem.SPSSType
		bolSelected = objItem.Selected
		If intItemType = SPSSPivot And bolSelected Then 
			Set objPivotTable = objItem.ActivateTable()  	'Activate the pivot table.
			lngIndex = i
			bolFoundPivot  = True	                  	' We did find a pivot table.
			Exit For                                  	' Exit the loop.
        End If
	Next i

	If Not bolFoundPivot Then
		'If no pivot table has been selected.
		MsgBox "Select a pivot table (of the correct type for your Autoscript) before running this script.", vbExclamation
		GetFirstSelectedPivotForAutoscript = False
		Exit Function
	End If
	
	GetFirstSelectedPivotForAutoscript = True

End Function