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

时光总嘲笑我的痴心妄想 提交于 2020-03-13 05:34:26

问题


Im trying to run a macro code but since I'm using a 64 bit Excel 2016 this code is not working. 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

回答1:


These should work on 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 run on both you can use the following #If VBA7

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

A nice resource for PtrSafe Win32 API declares can be found here: Win32API_PtrSafe.txt


I'm not quite sure about the IIDFromString and AccessibleObjectFromWindow but I think they should be subs instead of functions. And lpsz should be String like below. Maybe anyone 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)



回答2:


We need to do following two code changes:

  1. Replace "Long" data type with "LongPtr", at all places in the Script
  2. You need to change the Private Function Declarations as below

    • OLD: Private Declare Function GetTimeZoneInformation Lib "kernel32" ( _ lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    • NEW:

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

This is working For me.

Thanks.



来源:https://stackoverflow.com/questions/42557610/how-to-convert-32-bit-vba-code-into-64-bit-vba-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!