如何转换32位Excel VBA代码以在64位Excel上运行?

时间:2018-07-04 12:25:26

标签: excel vba excel-vba

我可以在办公室的Excel 2007中使用这些代码,但是为什么我不能在Excel 2016中使用它们?

它说它不是基于64位的,但是如何转换它呢?下面的代码以红色突出显示。

Private Declare Function FindWindow Lib "User32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetWindowLong Lib "User32" _
Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "User32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Declare Function DrawMenuBar Lib "User32" ( _
ByVal hwnd As Long) As Long

错误如下所示: Error

1 个答案:

答案 0 :(得分:4)

我发现x64系统具有这种等效性:

Private Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr


#If Win64 Then
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" _
    Alias "GetWindowLongPtrA" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long) As LongPtr
#Else
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" _
    Alias "GetWindowLongA" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long) As LongPtr
#End If

Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" _
Alias "SetWindowLongPtrA" ( _
ByVal hWnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr

Public Declare PtrSafe Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As LongPtr) As Long

来源:https://www.jkp-ads.com/articles/apideclarations.asp

我没有尝试过这些功能,只是经过了这段代码,尽管您的答案中使用了您的版本,它也没有用红色突出显示。