PDA

View Full Version : Userform control's handle



malik641
07-24-2008, 11:08 AM
Hey guys,

I've looked up and down, for 2 days, on how to obtain a control's handle on a userform in VBA...and have yet to find an answer.

I've looked at FindWindow and FindWindowEx APIs as well as EnumChildWindows (with GetWindowText and GetWindowTextLength) and I'm just either not understanding what's out there on the net or I'm not going the right path.

If I have a Userform with a TextBox, ListBox, and 2 CommandButtons, for example, does anybody know how to get each control's Handle?

I guess nobody does this in VBA...In VB, each control has a hWnd property. So does that mean that controls in VBA are Windowless?

Bob Phillips
07-25-2008, 03:18 AM
Joseph,

Not all form controls have handles, most MSForms are windowless contrrols. Only the Listbox and Frame controls have distinct handles, all others share the same hWnd.

I have some code that can get the handle, but unless it is a listbox or a frame it is not of much use.

malik641
07-25-2008, 07:09 AM
Thanks for the info, Bob. It's the most useful piece of information I've found yet.

The thing is, I'm trying to help someone on another board and, for some reason, they want to be able to programmatically "push" a commandbutton on a userform in VBA. I know I could do it if I can use SendMessage and set BM_SETSTATE to true for it to be "highlighted" (pushed in), but the problem with SendMessage is I need a handle:
http://msdn.microsoft.com/en-us/library/bb761823(VS.85).aspx (http://msdn.microsoft.com/en-us/library/bb761823%28VS.85%29.aspx)

I like the question because it's an interesting challenge...but I'm running out of choices now.

Are some controls in VB6/VB.NET also Windowless like VBA's controls?

Bob Phillips
07-25-2008, 08:29 AM
Thanks for the info, Bob. It's the most useful piece of information I've found yet.

The thing is, I'm trying to help someone on another board and, for some reason, they want to be able to programmatically "push" a commandbutton on a userform in VBA. I know I could do it if I can use SendMessage and set BM_SETSTATE to true for it to be "highlighted" (pushed in), but the problem with SendMessage is I need a handle:
http://msdn.microsoft.com/en-us/library/bb761823(VS.85).aspx (http://msdn.microsoft.com/en-us/library/bb761823%28VS.85%29.aspx)


Couldn't they just call the button's click event, same net effect?


I like the question because it's an interesting challenge...but I'm running out of choices now.

Are some controls in VB6/VB.NET also Windowless like VBA's controls?

I can't say for sure about VB.Net, but I think all VB6 controls are not windowless (windowed?), so I would guess the same is true in .Net.

malik641
07-25-2008, 08:36 AM
I suggested just that, but the problem with calling the _Click() event is they said that the button doesn't "visually" push in and come back out and they want it to....weird on WHY they need that, but again it's a cool challenge for me :)


And just looked at the intellisense in VB.NET for some controls, each has its own handle property:
Me.cmbTable.Handle
Me.TextBox1.Handle
Me.Label1.Handle
Me.lstAvailableColumns.Handle
Me.tabMain.Handle
I'll verify later if they do return distinct handle numbers.

Norie
07-25-2008, 11:02 AM
Why not just use a toggle button?

malik641
07-25-2008, 11:37 AM
I don't know. It's not my project. The user specifically requested to "push" a command button using code only (as if it was left-clicked by the mouse and they held down the button).

TomSchreiner
08-02-2008, 05:56 PM
Hi Josheph.

My "off the top of my head" approach would be to use the SendInput API function. You would need to determine the screen coords of the button on the userform. Some functions/properties you may need.

GetWindowPlacement API to determine the position of the userform in screen coords.

UserForm.InsideHeight and/or UserForm.InsideWidth. You can determine the width of the userform border using these in conjuction with the height and width properties.

GetClientRect API function.
ClientToScreen API function.

Conversion of points to pixels using a custom function.

Ect...

malik641
08-03-2008, 07:12 AM
Hey Tom,

Thanks for the suggestion. I will look into that. It just seems like so much work for something that is not so difficult in VB 6.0+.

I'm going to try to verify that I cannot get the handles of VBA userform controls. If there is no way to get them, I'll probably start using your alternative.

It's just so sad that I only need the handle and then I could use the SendMessage API. But I can't get this handle!

Bob Phillips
08-03-2008, 10:20 AM
You won't get handles of userform controls, as I said, the majority share the same handle.

Here's a thought, create a VB6 form and call that from VBA.

malik641
08-04-2008, 05:32 AM
Hey Bob,

Sorry about that. I was mistaken when I re-read post #4 and confused it with post #2.

And that's definitely a good idea. I'll look into that as well. I'll post my findings.

Bob Phillips
08-04-2008, 06:57 AM
It is easy, I do it when I want a menu userform as that is easy in VB, difficult in VBA>