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

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

问题描述

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

 私有声明函数ShellExecute Libshell32.dll别名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)。 I
假设这是因为
声明声明需要更新。



ve已经看到Office 2010推出了
a新的VBA运行时(VB7),并且这个
有一些新的关键字可以在$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>语句允许
在64位Windows上正常工作。
VB7还有新的预定义编译器
常量来支持条件
编译,其中将使用旧的或
的新声明,
取决于应用程序
在32或64位Windows上运行。



但是,由于我被卡住了Office
2007,我需要一个替代解决方案。
我的选择是什么? (如果
完全可以,我实际上是
不需要释放2
的单独版本)。


然而,根据David下面的回答,我误会了我的 Declare 语句无法正常工作的情况。在Windows 64位上的Office 2010 64位的唯一情况将不起作用。所以,Office 2007不是一个问题。

解决方案

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



我所要做的只是改变代码行:

 私有声明函数ShellExecute Libshell32.dll别名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

To This:

 #如果VBA7然后
私有声明PtrSafe函数ShellExecute Libshell32.dll别名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
私有声明函数ShellExecute Libshell32.dll别名ShellEx的ecuteA_
(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

当然,您将确保您正在使用的图书馆在两台机器上都可用,但是我没有使用过的一直是一个问题。



请注意,在旧的VB6中,PtrSafe甚至不是一个有效的命令,所以它将以红色显示虽然你有编译错误,但实际上并不会给出错误,因为编译器将跳过if块的第一部分。





使用上述代码的应用程序在Office 2003上编译并运行完美,2007年和2010年32和64位。


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

I originally said:

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.

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.

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).

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.

解决方案

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

To This:

#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.

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.

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天全站免登陆