In VBA for MS Word 2010, how can I get Word to bring up a color palette dialog box so the user can pick a color?
There are tons of examples on how to do it
On x64 Word you must modify Ádám's code below as follows:
Option Explicit
Option Base 0
Private Type CHOOSECOLOR
lStructSize As LongLong
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As LongLong
lpCustColors As LongPtr
flags As LongLong
lCustData As LongLong
lpfnHook As LongLong
lpTemplateName As String
End Type
Private Declare PtrSafe Function MyChooseColor _
Lib "comdlg32.dll" Alias "ChooseColorW" _
(ByRef pChoosecolor As CHOOSECOLOR) As Boolean
Public Function GetColor(ByRef col As LongLong) As _
Boolean
Static CS As CHOOSECOLOR
Static CustColor(15) As LongLong
CS.lStructSize = Len(CS)
CS.hwndOwner = 0
CS.flags = &H1 Or &H2
CS.lpCustColors = VarPtr(CustColor(0))
CS.rgbResult = col
CS.hInstance = 0
GetColor = MyChooseColor(CS)
If GetColor = False Then Exit Function
GetColor = True
col = CS.rgbResult
End Function
Use the function for example with the TextColor
property of a Font
object:
Sub FontColorTest()
Dim col As LongLong
col = rgb(200, 100, 50)
GetColor col
Dim p As Word.Paragraph
Set p = ActiveDocument.Paragraphs(1)
p.Range.Font.TextColor.rgb = CLng(col)
End Sub
Please note, that the GetColor
function requires a parameter of type LongLong
whereas the TextColor.rgb
property is of type Long
.