N
newsbin.telenet.be
I want to open a new XL workbook and paste the recordset from a subform.
Then I want the user to close (and ev. save) the workbook.
Dim appXL As Excel.Application
Dim xlbook As Excel.Workbook
Dim rs As DAO.Recordset
Dim iCols As Integer
Set rs = Forms("frmPrijsEvol")("frmPrijsEvolsub").Form.RecordsetClone
Set appXL = New Excel.Application
With appXL
.Visible = True
Set xlbook = .Workbooks.Add()
.ActiveWindow.Caption = "Pasted from MS Access (" &
Application.CurrentObjectName & ") - " & .ActiveWindow.Caption
For iCols = 0 To rs.Fields.Count - 1
.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
.Range(Cells(1, 1), Cells(1, rs.Fields.Count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With
Set appXL = Nothing
This works fine the 1st time.
When the code is activated a 2nd time Access gives runtime error 1004:
Method 'cells' of object _ global failed.
I suppose somewhere in my code the object variable is not explicitaly
declared and even if the user closes XL, the first instance keeps on
running.
Pleaese help me with the correct code.
Tks,
Michel
Then I want the user to close (and ev. save) the workbook.
Dim appXL As Excel.Application
Dim xlbook As Excel.Workbook
Dim rs As DAO.Recordset
Dim iCols As Integer
Set rs = Forms("frmPrijsEvol")("frmPrijsEvolsub").Form.RecordsetClone
Set appXL = New Excel.Application
With appXL
.Visible = True
Set xlbook = .Workbooks.Add()
.ActiveWindow.Caption = "Pasted from MS Access (" &
Application.CurrentObjectName & ") - " & .ActiveWindow.Caption
For iCols = 0 To rs.Fields.Count - 1
.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
.Range(Cells(1, 1), Cells(1, rs.Fields.Count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With
Set appXL = Nothing
This works fine the 1st time.
When the code is activated a 2nd time Access gives runtime error 1004:
Method 'cells' of object _ global failed.
I suppose somewhere in my code the object variable is not explicitaly
declared and even if the user closes XL, the first instance keeps on
running.
Pleaese help me with the correct code.
Tks,
Michel