引用 VBA 不知道其名称/路径的应用程序
Reference VBA Application without knowing its name / path
我们有一个遗留的 Access 数据库,我们可以在其中访问前端和报告。我们正处于我们想要导出的位置,并通过 Excel 操作报告中的数据。但是,我们的问题是导出到 Excel 功能已被禁用。
我知道,如果我知道数据库后端的位置,我就可以通过 ADO 或 DAO 设置引用,但由于与原始开发人员没有联系,这不是'不可能。
我能想到的解决这个问题的唯一方法是是否有某种方法可以循环遍历当前打开的应用程序对象,也许是通过 Windows API?从那里设置参考。
这可能吗?
编辑
我已经设法从 Excel 中检索到 Access hWnd,但我似乎无法像使用 Excel 应用程序那样将 hWnd 传递给 FindWindowEx API。
经过一些工作,我得出了下面的工作代码。引用数据库的实际代码比我使用 API 和 hWnd 的尝试要小得多。
我已经包含了表格和字段的打印以证明它有效。我很惊讶这是多么简单......!
'Requires References To:
'Access Object Library
'DAO Object Library
Sub GetTables()
Dim obj As Object 'Access Application
Dim db As Database 'Database
Dim tbl As TableDef 'Table
Dim rs As Recordset 'Recordset
Dim strTable As String 'Table Name
Dim strSQL As String 'SQL for Recordset
Dim fld As Field 'Field
'Get The Currently Open Access Application Object
'Ensure There Is Only One Open To Return The Desired Result
Set obj = GetObject(, "Access.Application")
'Get The Database Of The Application
Set db = obj.CurrentDb
'Loop Tables In Database
For Each tbl In db.TableDefs
'Get Table Name
strTable = tbl.Name
'Ignore Hidden Access Tables
If Left(strTable, 4) <> "MSys" Then
'Print Name Of Table
Debug.Print strTable
'Create SQL For Recordset Of Table
strSQL = "SELECT " & strTable & ".* FROM " & strTable & ";"
'Create Recordset
Set rs = db.OpenRecordset(strSQL)
'Loop Fields In Recordset
For Each fld In rs.Fields
'Print Name Of Field
Debug.Print " " & fld.Name
Next fld
End If
Next tbl
End Sub
我们有一个遗留的 Access 数据库,我们可以在其中访问前端和报告。我们正处于我们想要导出的位置,并通过 Excel 操作报告中的数据。但是,我们的问题是导出到 Excel 功能已被禁用。
我知道,如果我知道数据库后端的位置,我就可以通过 ADO 或 DAO 设置引用,但由于与原始开发人员没有联系,这不是'不可能。
我能想到的解决这个问题的唯一方法是是否有某种方法可以循环遍历当前打开的应用程序对象,也许是通过 Windows API?从那里设置参考。
这可能吗?
编辑
我已经设法从 Excel 中检索到 Access hWnd,但我似乎无法像使用 Excel 应用程序那样将 hWnd 传递给 FindWindowEx API。
经过一些工作,我得出了下面的工作代码。引用数据库的实际代码比我使用 API 和 hWnd 的尝试要小得多。
我已经包含了表格和字段的打印以证明它有效。我很惊讶这是多么简单......!
'Requires References To:
'Access Object Library
'DAO Object Library
Sub GetTables()
Dim obj As Object 'Access Application
Dim db As Database 'Database
Dim tbl As TableDef 'Table
Dim rs As Recordset 'Recordset
Dim strTable As String 'Table Name
Dim strSQL As String 'SQL for Recordset
Dim fld As Field 'Field
'Get The Currently Open Access Application Object
'Ensure There Is Only One Open To Return The Desired Result
Set obj = GetObject(, "Access.Application")
'Get The Database Of The Application
Set db = obj.CurrentDb
'Loop Tables In Database
For Each tbl In db.TableDefs
'Get Table Name
strTable = tbl.Name
'Ignore Hidden Access Tables
If Left(strTable, 4) <> "MSys" Then
'Print Name Of Table
Debug.Print strTable
'Create SQL For Recordset Of Table
strSQL = "SELECT " & strTable & ".* FROM " & strTable & ";"
'Create Recordset
Set rs = db.OpenRecordset(strSQL)
'Loop Fields In Recordset
For Each fld In rs.Fields
'Print Name Of Field
Debug.Print " " & fld.Name
Next fld
End If
Next tbl
End Sub