ActiveX component can't create object or return reference t

sll810

Board Regular
Joined
Jun 29, 2007
Messages
86
I am trying to use VBA in access to open excel and copy a recordset in. I am using the code below.

Sub CopyRecordSet(InRecSet As Recordset)

Dim xlApp As Excel.Application, xlNewBook As Excel.Workbook, xlDestSheet As Excel.Worksheet
Dim fname As String
Dim fld As DAO.Field

Dim i As Integer

Set xlApp = GetObject(, "excel.application")
Set xlNewBook = xlApp.Workbooks.Add
Set xlDestSheet = xlNewBook.Worksheets.Add

i = 1
For Each fld In InRecSet.Fields
xlDestSheet.Cells(1, i).Value = fld.Name
i = i + 1
Next fld
xlDestSheet.Range("A2").CopyFromRecordset InRecSet

End Sub


I keep getting an error at the line :
Set xlApp = GetObject(, "excel.application")

The error states "ActiveX component can't crete object or return reference to this object (Error 429)"

I have enabled references, including Active X controls 2.1. Does anyone have any suggestion about why I can't get excel to open. THe first time I ran this code it worked, now it won't anymore....
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I found a solution.

Sub CopyRecordSet(InRecSet As Recordset)

Dim xlApp As Excel.Application, xlNewBook As Excel.Workbook, xlDestSheet As Excel.Worksheet
Dim fld As DAO.Field

Dim i As Integer
Set xlApp = New Excel.Application
xlApp.Visible = True


Set xlNewBook = xlApp.Workbooks.Add
Set xlDestSheet = xlNewBook.Worksheets.Add

i = 1
For Each fld In InRecSet.Fields
xlDestSheet.Cells(1, i).Value = fld.Name
i = i + 1
Next fld
xlDestSheet.Range("A2").CopyFromRecordset InRecSet


For i = xlNewBook.Sheets.Count To 2 Step -1
xlNewBook.Sheets(i).Delete
Next i



End Sub

Note I used :
Set xlApp = New Excel.Application
xlApp.Visible = True

instead of GetObject()
 
Upvote 0
Why not just use TransferSpreadsheet?
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top