我应该如何让我的VBA code与64位Windows兼容?

问题描述:

我在Excel 2007中开发的VBA应用程序,它包含以下code允许从 SHELL32访问的ShellExecute 功能.DLL

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)。一世   假定这是因为   声明声明需要更新。

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),并且此   有一些可以使用的新的关键字   在声明语句,使它   在64位Windows正常工作。   VB7也有新的predefined编译   常量支持条件   编辑在这里无论是旧的或   新的声明将被使用,   取决于是否应用   在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.

不过,因为我坚持了办公室   2007年,我需要一个替代的解决方案。   我有什么选择? (我真的   preFER不要有释放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).

不过,按照以下大卫的回答,我错了有关在何种情况下我的声明语句将无法正常工作。唯一的情况下它不会工作,是64位的Windows 64位Office 2010。所以,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.

我已经遇到了这个问题,使用新的64位机器我的内部工具与Office 2010人。

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

我不得不这样做是code喜欢这种改变行:

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.

应用2010 32位和64位。

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