Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

Color Picker for Word VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edward Thrashcort - 12 Sep 2005 18:01 GMT
I am about to try to write a sub that displays Words' colour-selector dialog
and returns the colour (code) selected

ISTM that this MUST have already been written?  Anyone know of a source?

Eddie
Jay Freedman - 12 Sep 2005 19:28 GMT
> I am about to try to write a sub that displays Words' colour-selector
> dialog and returns the colour (code) selected
[quoted text clipped - 3 lines]
>
> Eddie

Hi Eddie,

Sure... I happened to have this one lying around. Make a userform with two
command buttons on it, CommandButton1 labeled "Choose Color" and
CommandButton2 labeled "Cancel". Then throw this code into it.

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

Dim CustomColors() As Byte

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

Private Declare Function FindWindow Lib "user32" _
   Alias "FindWindowA" (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long

Private Function GetWinwordHwnd() As Long
   Dim hWnd As Long

   hWnd = FindWindow("opusApp", vbNullString)
   GetWinwordHwnd = hWnd
End Function

Private Sub CommandButton2_Click()
   Unload Me
End Sub

Private Sub CommandButton1_Click()
   Dim cc As CHOOSECOLOR
   Dim lReturn As Long, Rval As Long
   Dim Gval As Long, Bval As Long
   Dim i As Integer

   cc.lStructSize = Len(cc)
   cc.hwndOwner = GetWinwordHwnd()
   cc.hInstance = 0
   cc.lpCustColors = StrConv(CustomColors, vbUnicode)
   cc.Flags = 0

   ' call the color picker
   lReturn = ChooseColorAPI(cc)
   If lReturn <> 0 Then
       ' extract the color values
       Rval = cc.rgbResult Mod 256
       Bval = Int(cc.rgbResult / 65536)
       Gval = Int((cc.rgbResult - (Bval * 65536) - Rval) / 256)

       ' display the values in the dialog title bar
       Me.Caption = "RGB Value User Chose: R=" & Str$(Rval) & _
            "  G=" & Str$(Gval) & "  B=" & Str$(Bval)
       ' change the dialog background to that color
       Me.BackColor = cc.rgbResult

       ' save the color values to send to the
       ' color picker for the next iteration
       CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
       ReDim CustomColors(0 To 16 * 4 - 1) As Byte
       For i = LBound(CustomColors) To UBound(CustomColors)
          CustomColors(i) = 0
       Next i
   Else
       MsgBox "User chose the Cancel Button"
   End If
End Sub

Private Sub UserForm_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

Private Sub UserForm_Layout()
   Me.Move 150, 100
End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Edward Thrashcort - 13 Sep 2005 00:29 GMT
Cheers Jay that works nicely.

Eddie

> > I am about to try to write a sub that displays Words' colour-selector
> > dialog and returns the colour (code) selected
[quoted text clipped - 92 lines]
>     Me.Move 150, 100
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.