Hello,
I'm having a problem with Run Time 1004 – Method worksheet of object global failed.
My VB opens an Excel template and copies data too it. It works every other time, debugging at ‘Set xlSheet = Worksheets("TestSheet")’ on the times it fails.
Can anyone see aht could be going wrong here?
This is a cut down piece of the code .
Thanks
[/COLOR][/FONT][/COLOR][/SIZE]
I'm having a problem with Run Time 1004 – Method worksheet of object global failed.
My VB opens an Excel template and copies data too it. It works every other time, debugging at ‘Set xlSheet = Worksheets("TestSheet")’ on the times it fails.
Can anyone see aht could be going wrong here?
This is a cut down piece of the code .
Thanks
Code:
[SIZE=3][COLOR=#000080][FONT=Arial]Public db As DAO.Database[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Public RS As DAO.Recordset[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Public xlApp As Object[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Public xlSheet As Object[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Private Sub REF_cmd1_Click()[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set db = CurrentDb[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set xlApp = CreateObject("Excel.Application")[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]xlApp.Visible = True[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]xlApp.Workbooks.Open "C:\Test.xlsm", True, False[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set xlSheet = Worksheets("TestSheet") 'Date Range[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set RS = CurrentDb.OpenRecordset("qry_QryTest1", dbOpenSnapshot)[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]xlSheet.Range("A1").CopyFromRecordset RS[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set xlSheet = Worksheets("Data") [/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set RS = CurrentDb.OpenRecordset("qry_ QryTest2 ", dbOpenSnapshot)[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]xlSheet.Range("A2").CopyFromRecordset RS[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]RS.Close[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set RS = Nothing[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set db = Nothing[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set xlApp = Nothing[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]Set xlSheet = Nothing[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000080][FONT=Arial]End Sub[COLOR=#000000]