确保您的VBA代码是64位兼容的

时间:2014-05-24 13:14:03

标签: excel function vba 32bit-64bit

我的Mircosoft Office 2010是x86但是当我尝试运行oe VBA时,我收到一个错误,它不兼容x64。

我有这段代码:

Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
    "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Declare Function PathIsNetworkPath Lib "shlwapi.dll" _
    Alias "PathIsNetworkPathA" ( _
    ByVal pszPath As String) As Long

Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" ( _
    ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

Private Declare Function SHEmptyRecycleBin _
    Lib "shell32" Alias "SHEmptyRecycleBinA" _
    (ByVal hwnd As Long, _
     ByVal pszRootPath As String, _
     ByVal dwFlags As Long) As Long

Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const MAX_PATH As Long = 260

Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As Long
    lpszProgressTitle As String
End Type

''''''''''''''''''''''''''''''''''''''
' Download API function.
''''''''''''''''''''''''''''''''''''''
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
                        "URLDownloadToFileA" ( _
                            ByVal pCaller As Long, _
                            ByVal szURL As String, _
                            ByVal szFileName As String, _
                            ByVal dwReserved As Long, _
                            ByVal lpfnCB As Long) As Long

我尝试将每个私有声明函数更改为私有声明PtrSafe函数

但是当我尝试加载代码时,我的Excel无法响应。

anyoe知道解决方案吗?

谢谢

求解器:只需将Long改为LongPtr

1 个答案:

答案 0 :(得分:6)

Long是32位整数但在64位版本的Office下调用Windows API句柄和指针将是64位宽,因此上面的API将失败。

在VBA7中提供了

LongPtr以适应这种情况,Microsoft的Compatibility Guide描述了在x86 / x64版本的Office下运行代码所需执行的操作。