
Function ESPORTA_IN_EXCEL()
On Error GoTo ESPORTA_IN_EXCEL_Err
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentData
Dim objXL As Object
Dim xlWB As Object
report_dir = "C:"
FileName = report_dir & "Report" & Format(Date, "yyyymmdd") & ".xls"
outputfilename = report_dir & FileName
For Each obj In dbs.AllQueries
DoCmd.OpenQuery obj.Name, acViewPivotTable, acEdit
'DoCmd.RunCommand acCmdPivotTableExportToExcel
DoCmd.TransferSpreadsheet acExport, 9, obj.Name, FileName, False
DoCmd.Close acQuery, obj.Name, acSaveNo
Next obj
ESPORTA_IN_EXCEL_Exit:
Exit Function
ESPORTA_IN_EXCEL_Err:
MsgBox Error$
Resume ESPORTA_IN_EXCEL_Exit
End Function |
Function ESPORTA_IN_EXCEL()
On Error GoTo ESPORTA_IN_EXCEL_Err
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentData
Dim strPath As String
Dim XlsApp As Object
Dim xlSheet As Object
Dim xlWorkbook As Object
report_dir = "C:"
strFileName = report_dir & "Report" & Format(Date, "yyyymmdd") & ".xls"
Set XlsApp = CreateObject("Excel.Application")
For Each obj In dbs.AllQueries
DoCmd.OpenQuery obj.Name, acViewPivotTable, acEdit
DoCmd.RunCommand acCmdPivotTableExportToExcel
XlsApp.Visible = True
Set xlSheet = XlsApp.Workbooks(1).Sheets(1)
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, obj.Name, FileName, True
'DoCmd.TransferSpreadsheet acExport, 9, obj.Name, FileName, False
DoCmd.Close acQuery, obj.Name, acSaveNo
XlsApp.Workbooks(1).SaveAs FileName:=strFileName
Next obj
ESPORTA_IN_EXCEL_Exit:
Exit Function
ESPORTA_IN_EXCEL_Err:
MsgBox Error$
Resume ESPORTA_IN_EXCEL_Exit
End Function |
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, obj.Name, filename, False
Option Compare Database
Option Explicit
Function ESPORTA_IN_EXCEL()
Dim report_dir As String, strfilename As String, obj As AccessObject, dbs As Object
Dim strPath As String, xlApp As Object, xlSheet As Object, xlWorkbook As Object, s As String
Const xlPivotTableVersion10 = 1
Const xlColumnField = 2
Const xlRowField = 1
Const xlSum = -4157
Const xlDatabase = 1
On Error GoTo ESPORTA_IN_EXCEL_Err
Set dbs = Application.CurrentData
report_dir = "G:"
strfilename = report_dir & "Report" & Format(Date, "yyyymmdd") & ".xls"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
For Each obj In dbs.AllQueries
DoCmd.OpenQuery obj.Name, acViewPivotTable
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, obj.Name, strfilename, True
Set xlWorkbook = xlApp.Workbooks.Open(strfilename)
With xlWorkbook
Set xlSheet = .sheets(1)
s = xlSheet.range("A1").currentregion.Address
.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=s).CreatePivotTable _
TableDestination:=xlSheet.Name & "!R1C6", TableName:="Tabella_pivot1", DefaultVersion:=xlPivotTableVersion10
.sheets(xlSheet.Name).Select
' Add the column field (the name of the pivot field is one of your data table) '
With .ActiveSheet.PivotTables("Tabella_Pivot1").PivotFields("Campo1")
.Orientation = xlColumnField
.Position = 1
End With
' Add the row field (the name of the pivot field is one of your data table) '
With .ActiveSheet.PivotTables("Tabella_Pivot1").PivotFields("Campo2")
.Orientation = xlRowField
.Position = 1
End With
' Add the data field
With .ActiveSheet.PivotTables("Tabella_Pivot1")
.AddDataField .PivotFields("Campo1"), "Sum of a value", xlSum
.AddDataField .PivotFields("Campo2"), "Sum of another value", xlSum
End With
End With
DoCmd.Close acQuery, obj.Name, acSaveNo
xlApp.ActiveWorkbook.SaveAs FileName:=strfilename
Next obj
ESPORTA_IN_EXCEL_Exit:
xlApp.Quit
Set xlApp = Nothing
Exit Function
ESPORTA_IN_EXCEL_Err:
MsgBox Error$
DoCmd.Close acQuery, obj.Name, acSaveNo
Resume ESPORTA_IN_EXCEL_Exit
End Function |
