VBA CreateObject 无法在 64 位 Windows 上创建 ActiveX 组件

标签 vba dll com activex 32bit-64bit

我必须在 Visual Studio 2013 中编写一个程序,但它不起作用,因此我开始使用 Microsoft Excel 及其开发平台测试所有可能性。我从以下版本开始(由提供我需要使用的程序的公司提供):

Sub Main()

    Dim XServer As X2000.Server
    Dim XApp As X2000.Application
    Dim XSR As X2000.ScriptRoutines

    Set XServer = New X2000.Server
    Set XApp = XServer.AppConnection()    '<- This actually fires the application
    XApp.Visible = True

    Set XSR = XApp.ScriptCommands
    XSR.DATA_FILENAME = "C:\X2000\myFile.x"    ' <- Code blocks here

End Sub

这基本上启动了应用程序 X2000.exe,将我的代码连接到它,然后执行 DATA_FILENAME分配例程,通过加载 myFile.x在节目中。我需要启动应用程序,因为此时它的许可证已被检查,并且我没有任何其他选项可以使用。

现在,在这个版本中,代码在最后一行被阻塞,说“A dll is missing”,没有任何其他信息。我尝试后期绑定(bind) ScriptRoutines通过执行以下操作来反对:

Dim XServer As X2000.Server
Dim XApp As X2000.Application
Dim XSR As Object

Set XServer = New X2000.Server
Set XApp = XServer.AppConnection()
XApp.Visible = True

XSR = CreateObject("XApp.ScriptCommands")

这也不好用,但确实给了我更多信息:“ActiveX 控件无法加载对象”。我做了一些研究,通过“Dependancy Walker”发现X2000.exe取决于 MSVBVM60.DLLsystem32 中缺少文件夹。不过很好奇,因为我的系统是 64 位的,Excel 也是,我在 VS2013 上尝试了使用 32 位兼容性的代码,但代码仍然不起作用。
我安装了the service pack from Microsoft ,包含此文件的位置,才发现 system32原封不动……sysWoW64而是已经有一个 MSVBVM60.DLL ,因此我怀疑 X2000.exe仅用于 32 位系统:我错了。

我的一位同事可以执行用 MATLAB 代码编写的相同程序:

function [Data,eng_or_met] = Read_X2000_File_BB_2(sFileName)

XServer = actxserver('X2000.server');
XApp = XServer.AppConnection;
XApp.Visible = 1;
objX2000 = XApp.ScriptCommands;
objX2000.data_filename = "C:\X2000\myFile.x";

delete(XServer);

由于 actxserver function,此代码有效,我相信它可以处理这个兼容性问题。

如何使用 Excel/VS2013 做同样的事情?

编辑:

我尝试使用 omegastripes 建议的代码:

Dim XSR As Object
Set XSR = CreateObjectx86("XApp.ScriptCommands") ' create ActiveX via x86 mshta host

正在 CreateObjectx86函数linked here .我尝试了 If / Else条件,最后无论如何都不会创建对象,两者都有:

Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID)



Set CreateObjectx86 = CreateObject(sProgID)

最后一行是如果我不更改代码(因此不是 #If Win64 )将执行的内容。

编辑:

根据这个问题的第一个答案,我也尝试过:

Dim XServer As Object
Dim XApp As X2000.Application
Dim XSR As X2000.Script

Set XServer = CreateObject("X2000.Server")
Set XApp = XServer.AppConnection()
XApp.Visible = True
Set XSR = X2000.ScriptCommands

只是发现我得到了同样的错误。

最佳答案

我显然解决了这个问题。受到 Hans Passant 的启发,我 Dim编辑 XServer作为唯一的对象取决于对 X2000.exe 的引用:

Dim XServer As X2000.Server
Dim XApp As Object
Dim XSR As Object

Set XServer = New X2000.Server
Set XApp = XServer.AppConnection
XApp.Visible = True
Set XSR = XApp.ScriptCommands

XSR.DATA_FILENAME = "C:\foo.x" 
DATA_FILENAME实际上是一个被执行的“ScriptCommand”。

就那么简单。但是我不知道为什么会这样。

关于VBA CreateObject 无法在 64 位 Windows 上创建 ActiveX 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45834233/

相关文章:

vba - Excel VBA - 仅复制和粘贴可见的表格行

c# 哪里可以找到System.Windows.Controls.dll

windows - 构建 DLL 时;我应该链接到什么类型的 CRT?

python - 构建引用 .PYD 的 Python 包/模块的正确方法是什么?

Python Outlook 2007 COM 入门

excel - 如何简化;数十个具有相同基础 VBA 代码的 Excel 选项卡

sql - WHERE(一组条件)OR(一组第二个条件)OR ...优雅地写

.net - 如何在 Excel VBA 中使用 .NET 对象?

c++ - 为什么 CoCreateInstance 可以在完全相同的上下文中返回两个不同的 HRESULT?

com - 在wix中注册com dll