Hi all,
I have an Access 97 db which has to upload some data from various Excel 2000 workbooks running on Win 2K. I have some code which opens an instance of Excel (I have the Excel 9.0 object library referenced), opens various recordsets, does some transfer of data, closes all the recordsets and Excel. This is summarised below:
Dim appXL As Excel.Application
Dim sht As Worksheet
Dim db As Database
Dim rst As Recordset
Set appXL = CreateObject("Excel.Application")
For Each sht In Worksheets
If sht.Name <> "Summary" Then
sht.DELETE
End If
Next sht
Set db = CurrentDb
Set rst = db.OpenRecordset("tblTable")
' Do various work to transfer data
Set rst = Nothing
Set db = Nothing
Set sht = Nothing
appXL.Application.quit
Set appXL = Nothing
All this works fine the first time round, but if I try to run this routine, or any of several similar ones which upload different data, the code fails with the error:
1004 - Method 'Worksheets' of object '_Global' failed
This usually occurs in the "For Each sht in Worksheets" loop above, but can happen in other places.
Further investigation with Windows Tack Manager shows that an Excel process is still running. If I close Access, that Excel process also finishes, and when I restart my Access application, I can run it once again. If I close the Excel process in Task Manager without closing Access, I can also run my code one again.
It, therefore, appears that I am not closing Excel correctly through my code.
Various users run these routines, and the order may vary depending on when various files becoe available. I can't therefore, program all the activities to happen in sequence within one application.
Needless to say, the users are getting frustrated.
Does anyone know where I am going wrong with this or have any suggestions?
Aidan Hughes.
I have an Access 97 db which has to upload some data from various Excel 2000 workbooks running on Win 2K. I have some code which opens an instance of Excel (I have the Excel 9.0 object library referenced), opens various recordsets, does some transfer of data, closes all the recordsets and Excel. This is summarised below:
Dim appXL As Excel.Application
Dim sht As Worksheet
Dim db As Database
Dim rst As Recordset
Set appXL = CreateObject("Excel.Application")
For Each sht In Worksheets
If sht.Name <> "Summary" Then
sht.DELETE
End If
Next sht
Set db = CurrentDb
Set rst = db.OpenRecordset("tblTable")
' Do various work to transfer data
Set rst = Nothing
Set db = Nothing
Set sht = Nothing
appXL.Application.quit
Set appXL = Nothing
All this works fine the first time round, but if I try to run this routine, or any of several similar ones which upload different data, the code fails with the error:
1004 - Method 'Worksheets' of object '_Global' failed
This usually occurs in the "For Each sht in Worksheets" loop above, but can happen in other places.
Further investigation with Windows Tack Manager shows that an Excel process is still running. If I close Access, that Excel process also finishes, and when I restart my Access application, I can run it once again. If I close the Excel process in Task Manager without closing Access, I can also run my code one again.
It, therefore, appears that I am not closing Excel correctly through my code.
Various users run these routines, and the order may vary depending on when various files becoe available. I can't therefore, program all the activities to happen in sequence within one application.
Needless to say, the users are getting frustrated.
Does anyone know where I am going wrong with this or have any suggestions?
Aidan Hughes.