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 varExcelApplica tion
varFilename = "C:\MyFile. xls"
'EXPORT DATA
DoCmd.TransferS preadsheet acExport, acSpreadsheetTy peExcel9, "MyQuery",
varFileName, True
'OPEN EXCEL HIDDEN
Set varExcelApplica tion = CreateObject("E xcel.Applicatio n")
varExcelApplica tion.Visible = False
'OPEN DOCUMENT
Call Shell("EXCEL """ & varFileName, vbMinimizedFocu s)
Set varCurrentFile = GetObject(varFi leName & ".xls")
'RENAME SHEET
varCurrentFile. ActiveSheet.Nam e = "MyData"
'ADD NEW SHEET
varCurrentFile. Sheets.Add
'RENAME SHEET
varCurrentFile. ActiveSheet.Nam e = "PivotSheet "
'CREATE PIVOT
varExcelApplica tion.ActiveWork book.PivotCache s.Add(SourceTyp e:=1, SourceData:
="'MyData'!A:P" ).CreatePivotTa ble TableDestinatio n:="'PivotSheet '!R3C1",
TableName:="Why DontYouWork", DefaultVersion: =10
'SAVE AND CLOSE
varCurrentFile. Save
varCurrentFile. Application.Qui t
End Sub
--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200702/1