Solved

I need a color picker for use with Word VBA (2000 and up on Windows 2000 and XP).

Posted on 2006-07-23
16
901 Views
Last Modified: 2012-05-05
Hi all,

I need a color picker for use with Word VBA (2000 and up on Windows 2000 and XP). Like the WIndows/Office Colors dialog box.

It must let you choose a color and return the 24-bit value.

thanks,

avi
0
Comment
Question by:progtw
  • 7
  • 7
  • 2
16 Comments
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 17163242
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 17163243
Ah, sorry, you wanted to do this within VBA.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17163278
0
 

Author Comment

by:progtw
ID: 17164187
to rockiroads:

I think you have given me a link to utilities which allow me to pick a color from whatever *happens* to be in screen.  

I meant something else. What I meant was a color selector of the type that you see a lot in Windows and Office. There is a dialog box called Colors which allows you to select any color of the rainbow, or make your own by giving RGB values while seeing whast they look like in the sample window. And there are two tabs. One contains a preset pallette of "honeycomb" colored hex shapes. And the other tab gives you the full range of all colors and hues etc. We use thme to set font colors or fill colors. I am sure that this dialog box must be available vrom in Windows, but I don't know how to access it. I want to access such a function using VBA to return me the user's color selection.

tia

avi
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17164314
The links are to Active X controls in which you can make use of in code.

I know what you mean, the only way possibly is if you include the common api dialog


0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17164341
ok, thought occurred ,use of API's
so lets try the common dialog api as suggested

u could add it as a reference then use it as a datatype

or this this bit of code

'Add this lot in a macro

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

Private Declare Function ChooseColor Lib "comdlg32.dll" _
   Alias "ChooseColorA" _
  (lpcc As CHOOSECOLORSTRUCT) As Long


'Here is functon that opens dialog and displays result
Public Sub PickColor()


    Dim cc As CHOOSECOLORSTRUCT
    Dim dwCustClrs(0 To 15) As Long

   
    With cc
        .flags = &H100
        .flags = .flags Or &H1
        .lStructSize = Len(cc)
        .hwndOwner = 0
        .lpCustColors = VarPtr(dwCustClrs(0))
    End With
   
    If ChooseColor(cc) = 1 Then
        MsgBox "Colour Selected : " & cc.rgbResult
    End If
End Sub


0
 

Author Comment

by:progtw
ID: 17168798
So far so good it seems to work.

Am I supposed to be able to see an entry for comdlg32 in the VBA Object Browser?

avi
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17170515
I dont know if u have to add it as a reference.
But since the code I posted makes use of it as a API function instead, there is no reason to worry about references
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:progtw
ID: 17190489
Sorry about the delay.
You mean the function is a Windows "kernel" API function? (I don't know if the word "kernel" is the right way of saying this.)

avi
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17190968
Well kernel is more system. Windows is just full of lots of dll's. This code just making use of one of them. I believe this dll is shipped with windows as it contains common dialogs used by windows
0
 

Author Comment

by:progtw
ID: 17191085
'scuse me for picking your brains some more (well you are getting 500 points...), but pray explain to me, why do some things have to be "referenced" (ie add a reference to them) and other things, like functions in the Windows DLLs as you mention, do not require a reference to them?

Oh, and where can see a list of the functions in comdlg32.dll?

tia

avi
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17191353
I'll try to explain, I know why, I just aint that great in explaining
If you add a reference to a library, it means your code can access all the methods available in that dll
You cannot use something in that dll unless u reference it somehow


Where can a list of funcs be found in comdlg32? I dont know, I only have used it for a few things
0
 

Author Comment

by:progtw
ID: 17191427
thnx

>> You cannot use something in that dll unless u reference it somehow
But earlier you wrote, and I quote, "But since the code I posted makes use of it as a API function instead, there is no reason to worry about references".
?

tia

avi
0
 

Author Comment

by:progtw
ID: 17191519
Ok, now I think I understand. The API function declaration code you wrote (quoted below) is the reference:

Private Declare Function ChooseColor Lib "comdlg32.dll" _
   Alias "ChooseColorA" _
  (lpcc As CHOOSECOLORSTRUCT) As Long

?

avi
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17191539
Yes, I am sepcifically defining what function to use in that DLL

Sorry about the confusion with references earlier. I meant actually adding a reference (Tools/References) as opposed to specifying a DLL function to use via code
0
 

Author Comment

by:progtw
ID: 17191628
Thanks,

I finally got around to testing it out by pickin a color and using it to shade a selection in a doc. Works. Great!

avi
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
merge adobe pdf files 14 93
Can email be forwarded using contact only on Exchange 2007 8 39
Edit header 19 87
Free AVI to DICOM converter 8 141
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now