【发布时间】:2025-11-27 14:30:02
【问题描述】:
您好,我正在努力为 Excel 2010 64 位更新 VBA 代码。我已经检查了所有内容,包括 * 上的一篇信息性帖子:* Question 我明白我必须声明 PtrSafe 并在适用的情况下创建 LongPtr 和 LongLong,但我在私有函数代码的“.rgbResult”部分收到“编译错误。类型不匹配”。任何任何和所有的帮助将不胜感激。我的代码如下:
Option Explicit
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As ChooseColor) As LongPtr
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Type ChooseColor
lStructSize As LongPtr
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As LongPtr
lpCustColors As String
flags As LongPtr
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As String
End Type
#Else
'{{{This Section of Code works ok so i have excluded it to save space as its the same as above without the ptr}}}}}
#End If
#Else
'{{{This Section of Code works ok so i have excluded it to save space}}}}}
#End If
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function ShowColor Lib "comdlg32.dll" Alias "ShowColorA" (pShowColor As ShowColor) As LongPtr
Dim ChooseColorStructure As ChooseColor
Dim Custcolor(16) As LongPtr
Dim lReturn As LongPtr
On Error GoTo ErrEnd:
ChooseColorStructure.lStructSize = LenB(ChooseColorStructure)
ChooseColorStructure.hwndOwner = FindWindow("XLMAIN", Application.Caption)
ChooseColorStructure.hInstance = 0
ChooseColorStructure.lpCustColors = StrConv(Custcolor(16), vbUnicode)
ChooseColorStructure.flags = 0
If ChooseColor(ChooseColorStructure) <> 0 Then
ShowColor = ChooseColorStructure.rgbResult
Custcolor(16) = StrConv(ChooseColorStructure.lpCustColors, vbFromUnicode)
On Error GoTo 0
Else
ShowColor = -1
End If
ErrEnd:
End Function
#Else
'{{{This Section of Code works ok so i have excluded it to save space}}}}}
#End If
#Else
'{{{This Section of Code works ok so i have excluded it to save space}}}}}
End Function
【问题讨论】:
-
不知道这是否会做很多事情,但我很确定 findwindow 函数需要用
As LongPtr声明,而你用as Long来声明它 -
另外,在发布的问题中,选择颜色功能是:
ChooseColorAPI而不是你的:ChooseColor -
啊,我明白了。好的,我会进行更新。如果您或任何人看到其他任何东西,我真的很感激。我一直在使用客户机器进行测试,所以我现在无法检查。谢谢ssssss
-
是的,在其他帖子中,它们是不同的。我试图尽可能多地从那个中获取,但是我的代码仍然完全不同。
标签: vba excel 64-bit color-picker