我必须在 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.DLL
,system32
中缺少文件夹。不过很好奇,因为我的系统是 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/