我应该如何使我的 VBA 代码与 64 位 Windows 兼容? [英] How should I make my VBA code compatible with 64-bit Windows?

查看:29
本文介绍了我应该如何使我的 VBA 代码与 64 位 Windows 兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Excel 2007 开发的 VBA 应用程序,它包含以下代码以允许从 Shell32.dll 访问 ShellExecute 函数:

I have a VBA application developed in Excel 2007, and it contains the following code to allow access to the ShellExecute function from Shell32.dll:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

我原本是说:

显然应用程序不会在 64 位版本的 Windows 上编译(仍在使用 32 位 Office 2007).一世假设这是因为Declare 声明需要更新.

Apparently the application will not compile on a 64-bit version of Windows (still using 32-bit Office 2007). I assume that this is because the Declare declaration needs updated.

我读过 Office 2010 介绍一个新的 VBA 运行时 (VB7),并且这有一些可以使用的新关键字在 Declare 语句中允许它在 64 位 Windows 上正常工作.VB7 也有新的预定义编译器支持条件的常量旧的或将使用新的声明,取决于应用程序是否在 32 位或 64 位 Windows 上运行.

I've read that Office 2010 introduced a new VBA runtime (VB7), and that this has some new keywords that can be used in the Declare statement to allow it to work properly on 64-bit Windows. VB7 also has new predefined compiler constants to support conditional compilation where either the old or new declaration will be used, depending on whether the application is running on 32 or 64-bit Windows.

但是,由于我一直在使用 Office2007 我需要一个替代解决方案.我有哪些选择?(我真的宁愿不必发布 2我的应用程序的单独版本,如果尽可能).

However, since I'm stuck with Office 2007 I need an alternative solution. What are my options? (I'd really prefer not to have to release 2 separate versions of my application if at all possible).

但是,根据下面 David 的回答,我误会了我的 Declare 语句不起作用的情况.它不起作用的唯一情况是 Windows 64 位上的 Office 2010 64 位.因此,Office 2007 不是问题.

However, per David's answer below, I was mistaken about the circumstances in which my Declare statement won't work. The only circumstances under which it won't work is Office 2010 64-bit on Windows 64-bit. So, Office 2007 is not an issue.

推荐答案

我已经遇到过这个问题,有人在装有 Office 2010 的新 64 位计算机上使用我的内部工具.

I've already encountered this problem on people using my in-house tools on new 64 bit machines with Office 2010.

我所要做的就是像这样更改代码行:

all I had to do was change lines of code like this:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

为此:

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If

您当然希望确保您使用的库在两台机器上都可用,但到目前为止我使用的所有库都没有问题.

You will, of course want to make sure that the library you're using is available on both machines, but so far nothing I've used has been a problem.

请注意,在旧的 VB6 中,PtrSafe 甚至不是有效命令,因此它会显示为红色,好像您有编译错误,但实际上它不会给出错误,因为编译器会跳过if 块的第一部分.

Note that in the old VB6, PtrSafe isn't even a valid command, so it'll appear in red as though you have a compile error, but it won't actually ever give an error because the compiler will skip the first part of the if block.

使用上述代码的应用程序可以在 Office 2003、2007 和 2010 32 位和 64 位上完美编译和运行.

Applications using the above code compile and run perfectly on Office 2003, 2007, and 2010 32 and 64 bit.

这篇关于我应该如何使我的 VBA 代码与 64 位 Windows 兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆