Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert 32-bit excel VBA code to run on 64-bit excel?

Tags:

excel

vba

I am able to use these codes in my Excel 2007 at the office, but how come I cannot use it in my Excel 2016?

It says that it isn't built on 64-bit, but how do I convert it? The code below is highlighted in red.

Private Declare Function FindWindow Lib "User32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetWindowLong Lib "User32" _
Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "User32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Declare Function DrawMenuBar Lib "User32" ( _
ByVal hwnd As Long) As Long

Error is shown below: Error

like image 515
Pherdindy Avatar asked Jul 04 '18 12:07

Pherdindy


1 Answers

I found this equivalences for x64 systems :

Private Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr


#If Win64 Then
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" _
    Alias "GetWindowLongPtrA" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long) As LongPtr
#Else
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" _
    Alias "GetWindowLongA" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long) As LongPtr
#End If

Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" _
Alias "SetWindowLongPtrA" ( _
ByVal hWnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr

Public Declare PtrSafe Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As LongPtr) As Long

Source : https://www.jkp-ads.com/articles/apideclarations.asp

I didn't try the functions, I only past this code and it isn't highlighted in red despite of your version in your answer.

like image 159
JC Guidicelli Avatar answered Sep 24 '22 16:09

JC Guidicelli