A
Alastair MacFarlane
Dear All
I am trying to automate the creation of a pivot table,
whether from within Excel or through automation from
another package and always seem to get an error when
trying to load the ADO recordset data into the pivot cache
(see sample code below). If I add a pivotcache to the
pivotchaches collection I receive no error, but when I try
to Set objPivotcache.Recordset = rstRecordset then I get
an error. I am simply stumped. If the pivotcache object is
added to the pivotcaches collection, then should each
pivotcache not have an Index?
Can anyone help me?
Alastair
Sub CreatePivot()
On Error GoTo err_handler:
Dim SQLString As String
Dim cnnConn As ADODB.Connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command
Dim objPivotcache As Excel.Pivotcache
'RECORDSET OPENED AND POULATED WITH DATA
Set objPivotcache = xlApp.ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlExternal)
'THIS LINE HERE IS WHERE THE 1004 AUTOMATION ERROR OCCURRS.
Set objPivotcache.Recordset = rstRecordset
With objPivotcache
.CreatePivotTable TableDestination:=Range("B5"), _
TableName:="pivAccessNL"
End With
I am trying to automate the creation of a pivot table,
whether from within Excel or through automation from
another package and always seem to get an error when
trying to load the ADO recordset data into the pivot cache
(see sample code below). If I add a pivotcache to the
pivotchaches collection I receive no error, but when I try
to Set objPivotcache.Recordset = rstRecordset then I get
an error. I am simply stumped. If the pivotcache object is
added to the pivotcaches collection, then should each
pivotcache not have an Index?
Can anyone help me?
Alastair
Sub CreatePivot()
On Error GoTo err_handler:
Dim SQLString As String
Dim cnnConn As ADODB.Connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command
Dim objPivotcache As Excel.Pivotcache
'RECORDSET OPENED AND POULATED WITH DATA
Set objPivotcache = xlApp.ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlExternal)
'THIS LINE HERE IS WHERE THE 1004 AUTOMATION ERROR OCCURRS.
Set objPivotcache.Recordset = rstRecordset
With objPivotcache
.CreatePivotTable TableDestination:=Range("B5"), _
TableName:="pivAccessNL"
End With