Sub Main 'Purpose: apply Autofit to all Pivot Tables 'Assumptions: there is an open Output Doc (Navigator) 'Effects: adjust Pivot Table row and column widths 'Inputs: none 'Return Values: none Dim objDocuments As ISpssDocuments ' SPSS documents. Dim objOutputDoc As ISpssOutputDoc ' Output document Dim objItems As ISpssItems ' Output Navigator items Dim objPivotTable As PivotTable ' The Pivot Table 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. ' Omitting this test results in a error message. If objDocuments.OutputDocCount > 0 Then 'Get the currently designated output document. Set objOutputDoc = objSpssApp.GetDesignatedOutputDoc Else 'If no navigator window exists, quit the script. 'comment the following line out and the script will go away silently. MsgBox "Please open an output window before running this script.", vbExclamation, "Script Error" Exit Sub End If ' Get the outline tree from the Navigator. Set objItems = objOutputDoc.Items ' Get each item in the Navigator. For i = 0 To objItems.Count - 1 Set objItem = objItems.GetItem(i) 'Get each item in turn. If objItem.SPSSType = SPSSPivot Then 'Check to see if it's a PivotTable Set objPivotTable = objItem.ActivateTable() 'Activate the pivot table. objPivotTable.UpdateScreen = False 'Defer drawing until later. '************************************************************* '* Insert the body of your script here, replacing everything '* between the rows of asterisks. 'Here's where we do the work: objPivotTable.Autofit '************************************************************* 'do all the drawing at once objPivotTable.UpdateScreen = True 'Clean-up time: Always remember to Deactivate when finished. 'note that it's the Item, not the Pivot Table, which is deactivated, 'just as it was the Item that was Activated. objItem.Deactivate End If Next End Sub