将 Excel 2003 和 2007 引用到 Autocad-VBA 项目中
我正在为 AutoCAD 编写 VBA(Visual Basic for Applications)脚本。它使用 Autocad 作为图形引擎,使用 Excel 来显示结果......
问题是,一些用户使用 Excel 2003,另一些用户使用 Excel 2007。
要从 VBA 使用 Excel 2003,我必须将 C:\Program Files\Microsoft office\Office12\excel.exe
引用到项目中。但要使用 Excel 2007,我必须引用 ...\Office14\excel.exe
。
对于不知道的人来说,引用是什么:它必须在项目属性内永久完成,所以我无法以编程方式决定这两个文件中的哪一个确实存在于计算机中并在运行时引用它们...... .或者也许我只是不知道,怎么办?
我什至无法同时引用这两个文件,因为它们具有相同的文件名。
I am writing a VBA (Visual Basic for Applications) script for AutoCAD. It uses Autocad as a graphical engine and Excel for displaying the results....
The problem is, that some of the users are using Excel 2003, and the other are using Excel 2007.
To use Excel 2003 from the VBA, I have to reference C:\Program Files\Microsoft office\Office12\excel.exe
to the project. But to use Excel 2007, I have to reference ...\Office14\excel.exe
.
For people that does not know, what reference is: it must be done permanently, within the project properties, so I can't programatically decide, which of that two files does exist in computer and reference them in runtime.... or maybe I just don't know, how?
I can't even reference both of the files at once, because they have the same filename.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用后期绑定 (IDispatch)。通过调用 VBA 的 CreateObject 方法创建 Excel 应用程序对象。这将使用安装的任何版本。
通过后期绑定,您将无法享受 IntelliSense 的便利。
不同版本的 Excel 具有不同的功能,在某些情况下甚至以不同的方式实现相同的功能。因此,请务必针对这两个版本进行测试。在某些地方,您可能必须为不同的版本使用不同的代码。
You'll need to use late binding (IDispatch). Create your Excel app object by calling VBA's CreateObject method. This will use whichever version is installed.
With late binding you won't have the convenience of IntelliSense.
Different versions of Excel have different features, and in some cases even implement the same feature in different ways. So be sure to test against both versions. In some places you may have to have different code for the different versions.
您需要后期绑定,即两者都不引用,将所有内容声明为“对象”并使用 CreateObject 实例化实例。
这并不难,因为您可以使用(早期绑定)中的引用进行编码,然后只需更改变量声明并删除引用即可。由于版本不同的问题,在针对 Excel 进行编码时,这种情况很常见。
请注意,您应该对这两个版本进行彻底测试,以防版本之间的 API 或功能行为发生变化。
You need to late bind, that is, reference neither, declare everything as "object" and use CreateObject to instantiate instances.
This isn't so hard as you can code with the reference in (early bound) and then just change the variable declarations afterwards and remove the reference. Its quite common when coding against excel due to the problem of different versions.
Note that you should test thoroughly against both incase there are changes in API or functional behaviour between the versions.
如果您正在为 AutoCAD 编写新代码,我会认真考虑转向 .NET。 Autodesk 正在逐步取消对 VBA 的支持 - 它尚未自动包含在 AutoCAD 的最后 2 个(很快将是 3 个)版本中:http://withoutanet.typepad.com/without_a_net/2009/04/vba-in-autocad-2010.html
当用户更新到更新版本时版本中,他们使用 VBA 附加组件会遇到更多阻力。语言与语言ARX 在未来几年内不会有任何发展,因为许多 AutoCAD 命令都是用这些语言编写的。
即使您正在维护现有代码,我认为您也应该认真努力迁移到 AutoCAD 在不久的将来支持的技术。我有很多用 VBA 编写的工具,我已经停止维护(阅读 - 放弃)和现在正在用.NET重写。从 VBA 到 VB 的飞跃并不是一个巨大的飞跃。就我个人而言,我现在更喜欢 C#,但这完全是个人选择。这些语言的功能是相同的。上面的链接和 http://through-the-interface.typepad.com/through_the_interface/2010/02/updated-devtv-autocad-vba-to-vbnet-migration-basics.html 。作为奖励,这些技能在未来和现在在 AutoCAD 开发之外将比 VBA 更有价值。
ODA 也支持 .NET 代码,因此使用其库将 AutoCAD 附加组件移植到独立应用程序(有点)简单。
此外,这可以解决您的问题 - 可以在 Visual Studio .NET 项目中以编程方式管理引用。
If you're writing new code for AutoCAD I'd seriously look at going to .NET. Autodesk are phasing out their support of VBA - it has not been automatically included in the last 2 (soon to be 3) versions of AutoCAD: http://withoutanet.typepad.com/without_a_net/2009/04/vba-in-autocad-2010.html
As users update to newer versions there will be even more friction for them to use VBA add-ons. LISP & ARX aren't going anywhere in the next few years since a lot of AutoCAD commands are written in those languages.
Even if you are maintaining existing code I think you should seriously work towards migrating to the technology that AutoCAD will support in the near future. I have quite a few tools I wrote in VBA which I have ceased to maintain (read - abandoned) & am now rewriting in .NET. The leap from VBA to VB isn't a quantum one. Personally, I now prefer C# but that is an entirely personal choice. The languages are equivalent in their functionality. There are a few links to migration guides on the link above and at http://through-the-interface.typepad.com/through_the_interface/2010/02/updated-devtv-autocad-vba-to-vbnet-migration-basics.html. As a bonus these skills will be a lot more valuable outside AutoCAD development than VBA in the future, and now.
.NET code is also supported by the ODA so it is (kinda) straightforward to port AutoCAD add-ons to a stand-alone app using their library.
Also, this could solve your problem - references can be managed programmatically in a Visual Studio .NET project.