Macro (SVB) Programs Example - Inserting All Open Windows into a Workbook

This example demonstrates how to create a new workbook and insert all of the currently open Statistica spreadsheets, reports, graphs, and macros into it. 

Note: Workbooks cannot be inserted into workbooks. 

Sub main
'Create a Workbook 
Dim WB As Workbook 
'Create a Workbook item which will represent 
'each type of document while they are being 
'inserted into the workbook 
Dim NewItem As WorkbookItem 
'Create a folder for each document type 
Dim SpreadsheetFolder As WorkbookItem 
Dim ReportFolder As WorkbookItem 
Dim GraphFolder As WorkbookItem 
Dim MacroFolder As WorkbookItem 
'Add the Workbook to the current list of open workbooks 
Set WB = Application.Workbooks.New 
'spreadsheets: 
'set the spreadsheet folder as a new folder inside of 
'the workbook and insert it at the top of the 
'workbook's hierarchy 
Set SpreadsheetFolder = WB.InsertFolder _ (WB.Root,scWorkbookFirstChild)
'Name the folder "Spreadsheets" SpreadsheetFolder.Name = "Spreadsheets" 
'Loop through all of the currently open spreadsheets 
'(individual windows and inside of other workbooks) by 
'using a For Each loop to go through the 
'Application.Spreadsheets collection 

For Each i In Application.Spreadsheets
'Insert each spreadsheet into the 
'spreadsheet folder that you just 
'inserted into the Workbook 
Set NewItem = WB.InsertObject _ (i,SpreadsheetFolder,)
'Close individual windows when the insertion 
'is complete i.CloseNext 'reports: 
'set the report folder as a new folder inside of the 
'workbook and insert it after the spreadsheet folder 
Set ReportFolder = WB.InsertFolder _ (SpreadsheetFolder,scWorkbookNextSibling)
ReportFolder.Name = "Reports" 
'Loop through all of the currently open reports 
'(individual windows and inside of other workbooks) by 
'using a For Each loop to go through the 
'Application.Reports collection 

For Each i In Application.Reports
'Insert each report into the 
'report folder that you just 
'inserted into the Workbook Set NewItem = WB.InsertObject(i,ReportFolder) i.CloseNext 
'graph: 
'set the graph folder as a new folder inside of the 
'workbook and insert it after the report folder 
Set GraphFolder = WB.InsertFolder(ReportFolder,scWorkbookNextSibling) 
GraphFolder.Name = "Graphs" 

'Loop through all of the currently open graphs 
'(individual windows and inside of other workbooks) by 
'using a For Each loop to go through the 
'Application.Graphs collection 
For Each i In Application.Graphs
'Insert each graph into the 
'graph folder that you just 
'inserted into the Workbook Set NewItem = WB.InsertObject(i,GraphFolder) 
i.CloseNext 
'macros: 'set the macro folder as a new folder inside of the 
'workbook and insert it after the graph folder 
Set MacroFolder = WB.InsertFolder _ (GraphFolder,scWorkbookNextSibling)
MacroFolder.Name = "Macros" 

'Loop through all of the currently open macros 
'(individual windows and inside of other workbooks) by 
'using a 

For Each loop to go through the 
'Application.Macros collection 
For Each i In Application.Macros
'Insert each macro into the 
'macro folder that you just 
'inserted into the Workbook 
Set NewItem = WB.InsertObject(i,MacroFolder)
Next 
WB.Visible = True
End Sub