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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

sll810

Board Regular
Joined
Jun 29, 2007
Messages
86
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()
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Why not just use TransferSpreadsheet?
 

Forum statistics

Threads
1,181,056
Messages
5,927,864
Members
436,573
Latest member
CMR237

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
Top