+ Reply to Thread
Results 1 to 14 of 14

Minimize the userform and give focus to the immediate next window, when button is clicked

Hybrid View

CaptainCool Minimize the userform and... 05-10-2014, 04:08 AM
playaller Re: Minimize the userform and... 05-10-2014, 06:25 AM
CaptainCool Re: Minimize the userform and... 05-10-2014, 06:32 AM
playaller Re: Minimize the userform and... 05-10-2014, 06:41 AM
CaptainCool Re: Minimize the userform and... 05-10-2014, 07:36 AM
Norie * Deleted * 05-10-2014, 07:55 AM
CaptainCool Re: Minimize the userform and... 05-10-2014, 07:58 AM
CaptainCool Re: Minimize the userform and... 05-10-2014, 09:50 AM
Leith Ross Re: Minimize the userform and... 05-10-2014, 11:59 AM
CaptainCool Re: Minimize the userform and... 05-10-2014, 12:10 PM
Leith Ross Re: Minimize the userform and... 05-10-2014, 12:42 PM
CaptainCool Re: Minimize the userform and... 05-10-2014, 06:29 PM
Leith Ross Re: Minimize the userform and... 05-10-2014, 08:36 PM
CaptainCool Re: Minimize the userform and... 05-10-2014, 11:04 PM
  1. #1
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Minimize the userform and give focus to the immediate next window, when button is clicked

    I currently have a small userform placed at the bottom right of the screen (with a command button on it) and this opens up on top of excel instances and other applications/windows and it is set so that you can't interact with anything except the userform.

    I wanted to minimize the userform and give focus to immediate next window (can be excel or non excel applications) for 10 seconds and then activate/show the userform back again, when the "Button 1" on the userform is clicked.

    I tried hiding the userform, but this doesn't give focus to the immediate next window. Below is the code

    Private Sub Button_Click()
        
        Me.Hide
        PauseApp (10)    
        Me.Show
        
    End Sub
    Can someone help me out with the code which does the below steps?? I googled for this, I could only find answers for adding minimize and maximize buttons to userform.

    When the button is clicked, it should perform

    Step a - Minimize the userform
    Step b - Give focus to the next immediate window behind the userform
    Step c - Wait for 10 seconds
    Step d - Bring back the userform its to state.

    - Captain Cool
    Last edited by Fotis1991; 05-10-2014 at 04:25 AM. Reason: use tags for your codes pls.

  2. #2
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    You can name the windows or ap windows if you know them.


    AppActivate "Microsoft Word"
    AppActivate "New Tab - Google Chrome"
    Example

    Private Sub Button_Click()
        
        Me.Hide
        AppActivate "Calculator"  Gives focus t the calculator if open
        PauseApp (10)    
        Me.Show
        
    End Sub
    Last edited by playaller; 05-10-2014 at 06:28 AM.


    Shelton A.
    If Helpful, Add Reputaion!

  3. #3
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Thanks Playaller, But the applications or App windows keeps on changing.

    Is there any other way so that we can give focus to the immediate next window?

    - Captain Cool

  4. #4
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Thats a hard one. I'll search for a workaround.


    Not what you need but may help:
    Alternatively you can try this:

    When the button is pressed the first time it will close the form and open a new one in vbmodeless mode and hide it so focus can be brought on the named window or you have time to select it yourself. Then it will pause for 10 second for interaction. After 10 seconds it will close the modeless and open the original form. You can capture data in between the switches.

    Sub Button_Click()
    
    unload Me
    
    UserForm1.Show vbModeless
    Me.Hide
    AppActivate "New Tab - Google Chrome"
    PauseApp (10)
    'do something
    Unload Me
    UserForm1.Show
    
    End Sub

  5. #5
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hi Playaller,

    Reason for looking for the code that gives the focus to the immediate next window is to take the alt print screen of the activte window. Currently as my userform has the focus, when I click on the capture button, it takes the print screen of my userform alone. But I wanted to give the focus to the immediate next window and then take the print screen of it.

    I've attached the code that I'm currently working on. Please have a look.

    Do you think of any other alternate where I could get the print screen of the active window?

    Also, for the post that you replied - http://www.excelforum.com/excel-prog...-word-doc.html

    I tried applying your suggested code, but it does seems to be working. Can you please help me out even here!

    Thanks a lot for your help so far!


    - Captain Cool
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    * Deleted *
    Last edited by Norie; 05-10-2014 at 08:11 AM.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    * deleted *
    Last edited by CaptainCool; 05-10-2014 at 09:43 AM.

  8. #8
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Is it possible to invoke ALT + TAB keys within VBA. I think it should be possible as can invoke 'Prt Scr' key. I'm not that great at code, can any one help me here!

    I wanted the below to happen when I click the capture button on my userframe.

    Step a - Minimize the userform >>> userframe.hide
    Step b - Give focus to the next immediate window behind the userform by invoking ALT + TAB keys here. Need help here!
    Step c - Wait for 10 seconds >>> pauseapp(10)
    Step d - Bring back the userform its to state >>> userframe.show

    - Captain Cool

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hello CaptainCool,

    I have code that will add the minimize, restore, and close X button to a userform so it behaves like a normal window. Would you be interested in this option?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Though currently I'm trying to add keybd_event alt tab simulation to my code. I'm happy to take/go with any other option that works!

    - Caption Cool

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hello CaptainCool,

    Step 1 of 2
    Add a new VBA Module to your workbook and paste this macro into it.
    ' Written: October 07, 2007
    ' Author:  Leith Ross
    ' Summary: Add Minimize, and Maximize/Restore buttons to a VBA UserForm
    
    Private Const GWL_STYLE As Long = -16
    Public Const MIN_BOX    As Long = &H20000
    Public Const MAX_BOX    As Long = &H10000
    
    Const SC_CLOSE      As Long = &HF060
    Const SC_MAXIMIZE   As Long = &HF030
    Const SC_MINIMIZE   As Long = &HF020
    Const SC_RESTORE    As Long = &HF120
    
    Private Declare Function SetWindowPos _
        Lib "user32.dll" _
            (ByVal hWnd As Long, _
             ByVal hWndInsertAfter As Long, _
             ByVal X As Long, _
             ByVal Y As Long, _
             ByVal cx As Long, _
             ByVal cy As Long, _
             ByVal wFlags As Long) _
        As Long
    
    Private Declare Function GetWindowLong _
        Lib "user32.dll" Alias "GetWindowLongA" _
            (ByVal hWnd As Long, _
             ByVal nIndex As Long) _
        As Long
                   
     Private Declare Function SetWindowLong _
        Lib "user32.dll" Alias "SetWindowLongA" _
            (ByVal hWnd As Long, _
             ByVal nIndex As Long, _
             ByVal dwNewLong As Long) _
        As Long
         
    'Redraw the Icons on the Window's Title Bar
     Private Declare Function DrawMenuBar _
        Lib "user32.dll" _
            (ByVal hWnd As Long) _
        As Long
    
    'Returns the Window Handle of the Window accepting input
     Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    
    Public Sub AddToForm(ByVal Box_Type As Long)
    
        Dim BitMask As Long
        Dim Window_Handle As Long
        Dim WindowStyle As Long
        Dim Ret As Long
    
            If Box_Type = MIN_BOX Or Box_Type = MAX_BOX Then
                Window_Handle = GetForegroundWindow()
      
                WindowStyle = GetWindowLong(Window_Handle, GWL_STYLE)
                BitMask = WindowStyle Or Box_Type
      
                Ret = SetWindowLong(Window_Handle, GWL_STYLE, BitMask)
                Ret = DrawMenuBar(Window_Handle)
            End If
    
    End Sub
    Step 2 of 2
    Add this code to your UserForm[/b]
    Private Sub UserForm_Activate()
        AddToForm MIN_BOX
       AddToForm MAX_BOX
    End Sub

  12. #12
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hi Leith Ross,

    Your code is absolutely perfect! I could add minimize and restore button to my userform. Thanks for the code

    But my issue/question remains still open => give focus to the immediate next window, when button is clicked.

    I think I need to compromise on my question. :'(


    - Captain Cool

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hello CaptainCool,

    Are you saying you have a command button on the UserForm when it is clicked should activate the next Window in the Z-Order? Or do you want the user to be able to select which application should be activated? Both cases are possible.

  14. #14
    Registered User
    Join Date
    05-09-2014
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Minimize the userform and give focus to the immediate next window, when button is clic

    Hi Leith Ross,

    Attached is the Excel that I'm working on.. Main intention of the Userform's in this workbook is to capture the screen shot of the active window (which I'm not able to do)!

    Let me explain you the steps that I wanted to do...

    a. Open the workbook, Userform FrameOne pops up and ask for a Start. Once the user clicks Start, FrameOne will be hidden and FrameTwo will be shown.
    b. FrameTwo has two button - Capture and Stop. FrameTwo is designed in such a way that it sits on top of all the windows (as it is/should a Modeless frame).
    c. Lets assume that I'm working some multiple applications, not sure which one are those - May be Office application or Non Office applications. And I wanted to capture the ALT Print Screen of a particular window/app. As my userform is not top of it, I'll click the 'Capture' button which should capture the screenshot after giving the focus to other window that I'm working on.

    Here's the problem! I'm not able to capture screen shot of the active window. Below is my code and as in the attached workbook.

    
    Sub screen_Capture()
     
      Call ClearClipboard
     
     'Capture Alt Print Screen == THIS IS NOT WORKING
    
         'Wanted to insert some code that gives focus to the immediate next window
         'Either ALT + TAB or some other logic  
    
         keybd_event VK_MENU, 0, 0, 0
         keybd_event VK_SNAPSHOT, 0, 0, 0
         keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
         keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
     
         WordObj.Selection.Paste ' RunTime Error 4605 is thrown pointing at this line
         WordObj.Selection.TypeText Chr(12)
         PauseApp (1)
     
     'Capture Full screen = THIS WORKS
        'keybd_event VK_SNAPSHOT, 0, 0, 0
        'WordObj.Selection.Paste
        'WordObj.Selection.TypeText Chr(12)
        'PauseApp (1)
      
    End Sub
    Kindly help me out as you said you can give the focus to window into both A Order and Z order.


    - Captain Cool
    Attached Files Attached Files
    Last edited by CaptainCool; 05-10-2014 at 11:43 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Userform Minimize Button Code Blocking Focus for First TextBox in Frame
    By excelforumkeys in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2014, 12:57 PM
  2. How to minimize the Excel window while the Macro is running ?
    By Ndt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2013, 03:56 PM
  3. Minimize userform and application window
    By dubbdan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-03-2013, 04:55 PM
  4. [SOLVED] Macro causing my window to minimize when clicked?
    By Bobp84 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2012, 11:17 AM
  5. Minimize workbook window
    By mqdias in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-21-2007, 10:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1