您好我正在努力为Excel 2010 64 Bit更新VBA代码。我已经检查了所有内容,包括StackOverflow上的信息性帖子:StackOverflow 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
答案 0 :(得分:2)
在搜索了一些之后,我很确定ChooseColor实际应该是as Long
而不是as LongPtr
我使用报价的引号进行了谷歌搜索:“Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As ChooseColor) As LongPtr
”
我只得到2个结果:这个问题,以及其他一些网站上的帖子,但我读不懂它,因为它是法语,我一直有意学习,但没有。
我搜索过:“Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As ChooseColor) As Long
”并且我得到了更多的结果,所以我认为它应该被声明为long而不是longPtr。即使在64中,仍然需要返回long而不是longPtr
此页面:http://www.jkp-ads.com/articles/apideclarations.asp?AllComments=True在评论中提到chooseColor函数,并且在那里用As Long
而不是longPtr声明它。以防你想要参考:)
答案 1 :(得分:1)
lStructSize As LongPtr
你要过分声明成员LongPtr而不是Long。只有当成员是指针或句柄类型时,才应使用LongPtr,而lStructSize不是指针。其他几个成员也是如此,包括rgbResult。它需要看起来像这样:
Private Type ChooseColor
lStructSize As Long
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As Long
lpCustColors As LongPtr
flags As Long
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As String
End Type