【问题标题】:How to convert 32 bit VBA code into 64 bit VBA code如何将 32 位 VBA 代码转换为 64 位 VBA 代码
【发布时间】:2017-07-22 07:23:39
【问题描述】:

我正在尝试运行宏代码,但由于我使用的是 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这些应该适用于 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 Long
    
    Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" _
      (ByVal Hwnd As LongPtr, ByVal dwId As Long, ByRef riid As GUID, _
      ByRef ppvObject As Object) As Long
    

    如果您需要它在两者上运行,您可以使用以下#If VBA7

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

    PtrSafe Win32 API 声明的一个很好的资源可以在这里找到:Win32API_PtrSafe.txt

    【讨论】:

    • 谢谢大家。这很有帮助,而且很有效。
    【解决方案2】:

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

    1. Long 数据类型替换为LongPtr,在所有位置 脚本
    2. 您需要将私有函数声明更改为 下面:
    • 旧:

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

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

    【讨论】:

    • 这对我有用,Win 11 MS 365
    • Replace Long data type with LongPtr - 即absolutely wrong。特别是,GetTimeZoneInformation 错误的是 returns a Long 而不是 LongPtr
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 2015-03-30
    • 2022-06-24
    • 1970-01-01
    • 2011-01-31
    • 2013-02-06
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多