How to convert 32-bit VBA code to 64-bit VBA code

I am trying to run macro code, but since I am using 64-bit Excel 2016, this code does not work. Please help me how to fix this.

Private Declare Function FindWindowEx Lib "User32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function IIDFromString Lib "ole32" _
(ByVal lpsz As Long, ByRef lpiid As GUID) As Long

Private Declare Function AccessibleObjectFromWindow Lib "oleacc" _
(ByVal hWnd As Long, ByVal dwId As Long, ByRef riid As GUID, _
ByRef ppvObject As Object) As Long
+4
source share
2 answers

They must work in 64-bit Excel

Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
  (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, _
  ByVal lpsz2 As String) As LongPtr

Private Declare PtrSafe Function IIDFromString Lib "ole32" _
  (ByVal lpsz As LongPtr, ByRef lpiid As GUID) As LongPtr

Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" _
  (ByVal Hwnd As LongPtr, ByVal dwId As LongPtr, ByRef riid As GUID, _
  ByRef ppvObject As Object) As LongPtr

If you need it to work on both devices, you can use the following #If VBA7

#If VBA7 Then
    '64 bit declares here
#Else
    '32 bit declares here
#End If

A good resource for PtrSafe Win32 API declarations can be found here: Win32API_PtrSafe.txt


I'm not quite sure about IIDFromStringand AccessibleObjectFromWindow, but I think they should be subsinstead functions. And it lpszshould be Stringas shown below. Maybe someone can confirm this?

Private Declare PtrSafe Sub IIDFromString Lib "ole32" ( _
  ByVal lpsz As String, ByRef lpiid As GUID)

Private Declare PtrSafe Sub AccessibleObjectFromWindow Lib "oleacc" _
  (ByVal Hwnd As LongPtr, ByVal dwId As LongPtr, ByRef riid As GUID, _
  ByRef ppvObject As Object)
+7

:

  1. "Long" "LongPtr"
  2. Script

    • OLD: GetTimeZoneInformation Lib "kernel32" (_     lpTimeZoneInformation As TIME_ZONE_INFORMATION)
    • NEW:

PtrSafe GetTimeZoneInformation Lib "kernel32" (_             lpTimeZoneInformation As TIME_ZONE_INFORMATION) As LongPtr

.

.

0

Source: https://habr.com/ru/post/1671255/


All Articles