Someone please help before i start to cry.
I'm trying to export from Access to Excel, then create a new excel sheet with
a pivot table to display the data held in columns A:P. I get the error
message "Run Time error 91: Object variable or with block variable not set"
on the "CREATE PIVOT" line below and can't find a way round.
Please someone. Help!
Option Compare Database
Option Explicit
Public Sub ftnMonitoring()
Dim varFileName As String
Dim varCurrentFile As Object
Dim varExcelApplication
varFilename = "C:\MyFile.xls"
'EXPORT DATA
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "MyQuery",
varFileName, True
'OPEN EXCEL HIDDEN
Set varExcelApplication = CreateObject("Excel.Application")
varExcelApplication.Visible = False
'OPEN DOCUMENT
Call Shell("EXCEL """ & varFileName, vbMinimizedFocus)
Set varCurrentFile = GetObject(varFileName & ".xls")
'RENAME SHEET
varCurrentFile.ActiveSheet.Name = "MyData"
'ADD NEW SHEET
varCurrentFile.Sheets.Add
'RENAME SHEET
varCurrentFile.ActiveSheet.Name = "PivotSheet"
'CREATE PIVOT
varExcelApplication.ActiveWorkbook.PivotCaches.Add (SourceType:=1, SourceData:
="'MyData'!A:P").CreatePivotTable TableDestination:="'PivotSheet'!R3C1",
TableName:="WhyDontYouWork", DefaultVersion:=10
'SAVE AND CLOSE
varCurrentFile.Save
varCurrentFile.Application.Quit
End Sub
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200702/1