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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42557610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:46:31  来源:igfitidea点击:

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

excel-vbavbaexcel

提问by April

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.

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

回答by P??

These should work on 64 bit Excel

这些应该适用于 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 you need it to run on both you can use the following #If VBA7

如果您需要它在两者上运行,您可以使用以下 #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

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



I'm not quite sure about the IIDFromStringand AccessibleObjectFromWindowbut I think they should be subsinstead of functions. And lpszshould be Stringlike below. Maybe anyone can confirm this?

我不太确定IIDFromStringAccessibleObjectFromWindow但我认为它们应该是subs而不是functions. 并且lpsz应该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)

回答by manoj remala

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:
  1. 在脚本的所有位置用“LongPtr”替换“Long”数据类型
  2. 您需要更改私有函数声明如下

    • 旧:私有声明函数 GetTimeZoneInformation Lib "kernel32" (_ lpTimeZoneInformation As TIME_ZONE_INFORMATION) 只要
    • 新的:

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

私有声明 PtrSafe 函数 GetTimeZoneInformation Lib "kernel32" (_ lpTimeZoneInformation As TIME_ZONE_INFORMATION) As LongPtr

This is working For me.

这对我有用。

Thanks.

谢谢。