将图标添加到用户窗体时类型不匹配
Type mismatch on Adding Icon to Userform
我一直在使用来自 this answer 的相同代码,并且每当我在互联网上搜索此代码时都会找到相同的代码,但我总是得到
type mismatch
在FindWindow
函数中Sub AddIcon
。也尝试了 this,下载了示例并得到了同样的错误。知道为什么吗?我正在使用 64 位版本。谢谢你。
在 64 位版本中 FindWindow
return 是 LongPtr
而不是 Long
(32 位)。
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As LongPtr '<-- FindWindow returns a LongPtr (for 64bit)
因此
hWnd = FindWindow(vbNullString, Me.Caption)
失败,因为 hWnd
在 AddIcon()
中被声明为 Long
但应该是 LongPtr
.
您可以使用
#If VBA7 Then
Dim hWnd As LongPtr
#Else
Dim hWnd As Long
#End If
根据 WinAPI 函数的声明,确保它适用于 32 位和 64 位版本。
注意:您可能会检查声明为 Long
的其他变量是否也需要更改为 LongPtr
。因此,只需查看 64 位声明以及函数 return.
我一直在使用来自 this answer 的相同代码,并且每当我在互联网上搜索此代码时都会找到相同的代码,但我总是得到
type mismatch
在FindWindow
函数中Sub AddIcon
。也尝试了 this,下载了示例并得到了同样的错误。知道为什么吗?我正在使用 64 位版本。谢谢你。
在 64 位版本中 FindWindow
return 是 LongPtr
而不是 Long
(32 位)。
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As LongPtr '<-- FindWindow returns a LongPtr (for 64bit)
因此
hWnd = FindWindow(vbNullString, Me.Caption)
失败,因为 hWnd
在 AddIcon()
中被声明为 Long
但应该是 LongPtr
.
您可以使用
#If VBA7 Then
Dim hWnd As LongPtr
#Else
Dim hWnd As Long
#End If
根据 WinAPI 函数的声明,确保它适用于 32 位和 64 位版本。
注意:您可能会检查声明为 Long
的其他变量是否也需要更改为 LongPtr
。因此,只需查看 64 位声明以及函数 return.