使用Access 2010 VBA列出所有打开的Excel工作簿
好吧,我一定错过了一些微妙的东西 在Excel 2010中,此VBA工作:使用Access 2010 VBA列出所有打开的Excel工作簿,vba,excel,ms-access-2010,Vba,Excel,Ms Access 2010,好吧,我一定错过了一些微妙的东西 在Excel 2010中,此VBA工作: Private Sub ExcelTest() Dim wb As Workbook For Each wb In Workbooks Debug.Print wb.Name Next wb End Sub 我试图在Access VBA中复制这一点,我在Access 2010中尝试了以下方法,但没有成功 Private Sub AccessTest1() 'Derived from http://st
Private Sub ExcelTest()
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
Next wb
End Sub
我试图在Access VBA中复制这一点,我在Access 2010中尝试了以下方法,但没有成功
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
我肯定在Access中签入了Microsoft Excel 14.0对象库引用。
我只是想列出所有打开的Excel工作簿,这样我就可以针对这些信息采取行动。
提前感谢您的帮助。Set xlApp=New Excel。该应用程序将创建一个新的Excel实例,当然您在其中没有任何工作簿 您想要实现的是遍历已打开的Excel实例。因此,您必须使用getObject 另一个错误在For语句中。。。您必须使用xlApp.Workbooks而不是Excel.Workbooks 以下代码应提供导出的结果:
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
还请记住,通过将对象变量设置为“无”,在末尾对其进行分解 谢谢,这很有效。我有一个想法,我必须在一个工作簿中,但我想不出如何实现这一点,而不选择一个工作簿。我还没来得及做清理工作,因为我没法做最基本的部分。再次感谢您的回答和解释。