如何将32位VBA代码转换为64位VBA代码

时间:2017-03-02 14:07:54

标签: excel-vba vba excel

我试图运行宏代码,但由于我使用的是64位Excel 2016,因此该代码无效。请帮我解决这个问题。

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

2 个答案:

答案 0 :(得分:7)

这些应该适用于64位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 VBA7

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

可以在此处找到PtrSafe Win32 API声明的一个不错的资源:Win32API_PtrSafe.txt

我对IIDFromStringAccessibleObjectFromWindow不太确定,但我认为它们应该是subs而不是functionslpsz应该是String,如下所示。也许有人可以证实这一点吗?

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)

答案 1 :(得分:1)

我们需要进行以下两个代码更改:

  1. 将“ Long”数据类型替换为“ LongPtr”, 脚本
  2. 您需要将私有函数声明更改为 下面

    • OLD: 私有声明函数GetTimeZoneInformation Lib“ kernel32”(_       lpTimeZoneInformation为TIME_ZONE_INFORMATION)
    • 新:

私有声明PtrSafe函数GetTimeZoneInformation库“ kernel32”(_               lpTimeZoneInformation作为TIME_ZONE_INFORMATION)作为LongPtr

这对我有用。

谢谢。