Excel Automation from Access problem

TheFrog

New Member
Joined
Aug 4, 2009
Messages
11
Hi Everyone,

I am having an issue with Excel automation that doesnt seem to make any sense. There appears to be a difference between early binding the code and late binding the code - and the late binding is behaving strangely.

In short I am using automation to open a workbook, check all the sheets in it for a specific data structure, and if it exists then return the data. Pretty simple it would seem. The complication is that the workbooks have a form on them that launches on the Workbook_Open event - so to get around this I set the Excel.Application objects .EnableEvents property to false. This works perfectly with early binding, but not with late binding, and I dont know why.:eek:

Can anyone help with this one?
Excel 2000 SP3 build: 9.0.8950

Cheers,

The Frog


Code is as follows:


Sub EarlyBinding()
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xl = New Excel.Application
xl.EnableEvents = False

Set wb = xl.Workbooks.Open("Filename<INSERT HERE FILENAME>")

For Each ws In wb.Worksheets
Debug.Print ws.Name
Next

wb.Close savechanges:=False

Set wb = Nothing
Set xl = Nothing

End Sub
__________________________________________________________
****************************************************
Late Binding (form still opens):

Sub LateBinding()
Dim xl As Object 'Excel itself
Dim wb As Object 'Workbook
Dim ws As Object 'Worksheets Collection
Dim ss As Object 'Spreadsheet (not set, just stays as object)

Set xl = CreateObject("Excel.Application")

xl.DisplayAlerts = False
xl.Application.DisplayAlerts = False
xl.Application.EnableEvents = False
xl.EnableEvents = False

'The state of the .EnableEvents property actually changes
'When the next line of code executes (to .EnableEvents = True)
Set wb = xl.Workbooks.Open("Filename<INSERT Here Filename>")

Set ws = xl.ActiveWorkbook.Sheets

For Each ss In ws
Debug.Print ss.Name
Next

Set ws = Nothing
wb.Close savechanges:=False

Set ws = Nothing
Set wb = Nothing
Set xl = Nothing

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thankyou for the information. What a bugger of a thing. I will control the scenario with reference setting via a class module instead.

Much appreciated

The Frog
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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