Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

vba - Excel 64-bit and comdlg32.dll custom colours

I'm trying to adapt the code in either here or here to open the custom colour palette in Excel 2010 64-bit but cannot get it to work. Code on both sites work fine in Excel 2003

One attempt

 Option Explicit

 Private Type CHOOSECOLOR
 lStructSize As Long
 hwndOwner As Long
 hInstance As Long
 rgbResult As Long
 lpCustColors As String
 flags As Long
 lCustData As Long
 lpfnHook As Long
 lpTemplateName As String
 End Type

 Private Declare PtrSafe Function ChooseColorAPI Lib "comdlg32.dll" Alias _
 "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long

 Dim CustomColors() As Byte

 Private Sub Command1_Click()
   Dim cc As CHOOSECOLOR
   Dim Custcolor(16) As Long
   Dim lReturn As Long
   cc.lStructSize = Len(cc)
   cc.hwndOwner = Application.Hwnd
   cc.hInstance = 0
   cc.lpCustColors = StrConv(CustomColors, vbUnicode)
   cc.flags = 0
   lReturn = ChooseColorAPI(cc)
   If lReturn <> 0 Then
       Application.Caption = "RGB Value User Chose: " & Str$(cc.rgbResult)
       Application.BackColor = cc.rgbResult            ' Visual Basic only ****
       Application.Section(0).BackColor = cc.rgbResult ' Access only **********
       CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
   Else
       MsgBox "User chose the Cancel Button"
   End If
   End Sub

   Private Sub Form_Load()
   ReDim CustomColors(0 To 16 * 4 - 1) As Byte
   Dim i As Integer

   For i = LBound(CustomColors) To UBound(CustomColors)
       CustomColors(i) = 0
   Next i
   End Sub

This runs ok but doesn't show the dialog. I've also tried changing some LONG types to LONGPTR with no success. Does anyone know how to get this working on a 64-bit machine; or if it's even possible? Perhaps there's a new library?

Thanks

Edit: Slight rewording with offer of bounty... How do I access and use this custom colour chooser (image below) in Excel 2010 64-bit (MUST work on 64-bit!) to set cells in Excel 2010 with the colour chosen and store the colour? The image is taken from Excel 2010 64-bit by selecting fill button>more colors>Custom

Valid XHTML http://img851.imageshack.us/img851/2057/unlednvn.png

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two things I would try. First, replace every use of Long with LongPtr.

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

Private Declare PtrSafe Function ChooseColorAPI Lib "comdlg32.dll" Alias _
    "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As LongPtr

Second, replace the use of Len with LenB.

Private Sub Command1_Click()
    Dim cc As CHOOSECOLOR
    Dim Custcolor(16) As LongPtr
    Dim lReturn As LongPtr

    cc.lStructSize = LenB(cc)
    cc.hwndOwner = Application.Hwnd
    cc.hInstance = 0
    cc.lpCustColors = StrConv(CustomColors, vbUnicode)
    cc.flags = 0
    lReturn = ChooseColorAPI(cc)

    If lReturn <> 0 Then
       Application.Caption = "RGB Value User Chose: " & Str$(cc.rgbResult)
       Application.BackColor = cc.rgbResult            ' Visual Basic only ****
       Application.Section(0).BackColor = cc.rgbResult ' Access only **********
       CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
    Else
       MsgBox "User chose the Cancel Button"

    End If
End Sub

Private Sub Form_Load()
    ReDim CustomColors(0 To 16 * 4 - 1) As Byte
    Dim i As Integer

    For i = LBound(CustomColors) To UBound(CustomColors)
       CustomColors(i) = 0
    Next i
End Sub

More Info

LongPtr Data Type

LenB Function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...