简体   繁体   中英

Using Access 2010 VBA list all open Excel Workbooks

Ok, I must be missing something subtle here.

In Excel 2010 this VBA works:

Private Sub ExcelTest()

Dim wb As Workbook

For Each wb In Workbooks

    Debug.Print wb.Name

Next wb

End Sub

I'm trying to replicate this in Access VBA and I've tried the following approaches in Access 2010 with no success.

Private Sub AccessTest1()
'Derived from http://stackoverflow.com/questions/15958061/controlling-excel-
'workbook-from-access-2010-vba

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

For Each xlWB In Excel.Workbooks
    Debug.Print xlWB.Name
Next xlWB

'Nothing happens.
'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'xlWB = Nothing
'xlWB.Name = Object variable or With block variable not set.
'But doesn't throw an error.

End Sub

Private Sub AccessTest2()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba

Dim xlApp As Excel.Application

Dim wb As Excel.Workbook

Set xlApp = New Excel.Application

For Each wb In Excel.Workbooks  '<--- Like this nothing happens
    Debug.Print wb.Name
Next wb

'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'wb = Nothing
'wb.Name = Object variable or With block variable not set.  But doesn't throw an error.

For Each wb In xlApp.Workbooks  '<--- This throws a "Object variable or 
                                 'With block variable not set" error
    Debug.Print wb.Name
Next wb

End Sub

Private Sub AccessTest3()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba

Dim objExcelApp As Object
Dim wb As Object

Set objExcelApp = CreateObject("Excel.Application")

Set wb = objExcelApp.Workbook  '<--- Throws "Object doesn't support this property
                               'or method error"
'Hovering after error:
'objExcelApp = "MicroSoft Excel"
'wb = Nothing

For Each wb In objExcelApp.Workbooks
    Debug.Print wb.Name
Next wb

End Sub

I definitely have the "Microsoft Excel 14.0 Object Library" reference checked in Access. I'm just trying to list any open Excel Workbooks so that I can then act against that information. Thanks for your help in advance.

Set xlApp = New Excel.Application creates a new instance of Excel - and of course you do not have any workbooks in there.

What you want to achive is to go thru an already opened Excel instance. Therefore you have to use getObject().

Another Error is in your For statement ... you have to use xlApp.Workbooks instead of Excel.Workbooks.

The following code should provide the exprected result:

Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")

Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
    Debug.Print xlWB.Name
Next xlWB

Set xlApp = Nothing
Set xlWB = Nothing

Please keep also in mind to destry object variables on the end by setting them to Nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM