Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure your VBA code is 64-bit compatible

My Mircosoft Office 2010 is x86 but when I try running oe VBA, I get an error that it is not x64 compatible.

I have this code:

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

I tried changing every Private Declare Function to Private Declare PtrSafe Function

But when I try loading the code, my Excel is getting unresponsive.

Does anyoe know about a solution?

Thank you

Solver: just needed to change Long to LongPtr too

like image 505
user3671745 Avatar asked May 24 '14 13:05

user3671745


1 Answers

Long is a 32 bit integer but under a 64 bit version of Office calling the Windows API handles and pointers are going to be 64 bits wide so the APIs above will fail.

LongPtr is provided in VBA7 to accommodate this, Microsoft's Compatibility Guide describes what you need to do to make you code run under x86/x64 versions of Office.

like image 168
Alex K. Avatar answered Sep 28 '22 12:09

Alex K.