View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Stephen Newman[_2_] Stephen Newman[_2_] is offline
external usenet poster
 
Posts: 18
Default Return Control to Userform

On Sun, 20 Jan 2008 19:13:57 -0000, "RB Smissaert"
wrote:

I kept searching and found a few references that I was able to
edit/combine and came up with this.

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx
As Long, _
ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As
Long)

Sub LeftClick()
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Sub RightClick()
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub

Sub CenterClick()
mouse_event MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
End Sub

I put the code in a module, called LeftClick after endIf in the
original code, and it did the trick for me. I'm not sure, but from
what I read while combining the codes I found, the four digits
represent co-ordinates of the mouse.

I'm not sure if the Putfocus would have worked or not as I alt/tabbed
through all the open programs I had running, and when I returned to
excel I was faced with the same issue.

I do appreciate your assistance, as your original suggestion did lead
me in the direction of a solution. Thank you. Problem solved.

Try something like this:

Option Explicit
Private Declare Function Putfocus _
Lib "user32" _
Alias "SetFocus" _
(ByVal hwnd As Long) As Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function GetFormHwnd(strCaption As String) As Long
If Val(Application.Version) = 9 Then
GetFormHwnd = FindWindow("ThunderDFrame", strCaption)
Else
GetFormHwnd = FindWindow("ThunderXFrame", strCaption)
End If
End Function

Sub SetFormFocus(strCaption As String)
Putfocus GetFormHwnd(Me.Caption)
End Sub


RBS


"Rumplestiltskin" wrote in message
.. .
On Sun, 20 Jan 2008 17:36:48 -0000, "RB Smissaert"
wrote:

Tried the setfocus on individual buttons, and cycled through them. No
luck. I haven't been successful in discovering a way to send a mouse
click. Tried several different keystrokes thinking I could possibly
use a SendKeys function, but none of the keyboard keys have any
effect.

I searched for putFocus, and didn't see anything I could understand.

Thanks for the ideas.

Have you tried doing SetFocus with one control and then another control?
Another option is to send a mouse-click with the API or use the Putfocus
API
on the form.

RBS


"Stephen Newman" wrote in message
...
I have a userform with the following code attached to an image:

Private Sub Image1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
IsKeyPressed
If Ctrl = True Then
MsgBox "You double clicked the image" & vbNewLine & "while holding the
Ctrl key.", vbInformation, "Control Return Issue"
DoEvents
Else: MsgBox "You double clicked the image.", vbInformation, "Control
Return Issue"
End If
End Sub

In both cases when the code has completed control is not returned to
the userform until the user mouse clicks the form once. There are
additional buttons on the form, but even when I add a setFocus command
following the msgbox, the button is focused, but still needs the extra
mouse click to be operable.

I have tried adding the DoEvents command, but it still does not return
control to the userform.

Am I missing something? If so, could somebody please point out where
I'm wrong?