如何在Excel VBA中创建MS Access运行时的对象

How to Create object of MS Access Runtime in Excel VBA

当我在Excel VBA中创建对象时,我的Microsoft Access Runtime不是完整版的Microsoft Access。

1
Set objAccess = CreateObject("Access.Application")

那时候我越来越

Error 429"ActiveX component can't create object."

建议如何创建对象?


我不确定此信息是否仍然与OP相关,但可能会帮助其他寻求解决方案的人(如我):

在简单路线

的情况下

1
2
Dim AccApp as Object
Set AccApp = CreateObject("Access.Application")

不起作用(例如,因为只有Access的运行时版本可用),以下路由似乎起作用:

1
2
3
4
5
6
7
8
9
10
Const PathToDBFile as String ="W:\\here\\Your\\DB\\lies.accdb"
Const PathToAccess as String ="C:\\Program files\\YourOfficeVersion\\MSACCESS.EXE"
Dim ShellCmd as String
' Piece together the parts (yes, the quotes are necessary in case there are spaces in the paths)
ShellCmd ="""" & PathToAccess &"""""" & PathToDBFile &""""
' Execute the command in the shell
VBA.Shell ShellCmd
' Now GetObject can return the newly created instance of Access
Dim AccApp as Object
Set objAcc = GetObject(PathToDBFile)

(来源)

此代码只是显示基本步骤的基本内容。可能要确保没有一个正在运行的Access实例。另外,我还没有弄清楚如何在不同系统上可靠地获取MSAccess.exe的路径。但是当我在仅安装了运行时版本的系统上尝试时,以上内容对我有用。 (我能够从AccApp.Run"MyFunction"获得正确的回报。)