Results 1 to 10 of 10

Thread: [RESOLVED] Display Excel Userform in 'unfocused' state

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    [RESOLVED] Display Excel Userform in 'unfocused' state

    Is it possible to display an Excel Userform such that is appears initially in a de-highlighted state? I'm not sure, but I think the term is 'focus'

    I currently show a modeless userform triggered from the workbook open event but it always appears in a highlighted state.

    VB Code:
    1. Private Sub Workbook_Open()
    2.     OptionList.Show vbModeless  'appears in highlighted state
    3. End Sub
    Last edited by VBAhack; Feb 18th, 2006 at 09:08 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Display Excel Userform in 'unfocused' state

    VB Code:
    1. Private Sub Workbook_Open()
    2.     OptionList.Show vbModeless
    3.     Application.WindowState = xlMinimized
    4.     Application.WindowState = xlMaximized
    5. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Display Excel Userform in 'unfocused' state

    RobDog,

    That's an interesting trick. Thanks!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Display Excel Userform in 'unfocused' state

    I know its very elegant but as long as your opening the file any flashing from it minimizing/maximizing will not be noticeable.

    You could also get fancy and use a couple of APIs to get the solution but more work then its worth unless your needing it to be inactive when showing a userform and not in combination of opening the file and showing the userform.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Display Excel Userform in 'unfocused' state

    Actually, the flashing is very noticeable. You can readily see the window being minimized, then maximized. If the API approach isn't too nasty I'd like to give it a try in the interest of elegance. Can you explain?

    P.S. I want the Userform to be inactive all the time except when clicked.
    Last edited by VBAhack; Feb 18th, 2006 at 04:46 PM.

  6. #6

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Display Excel Userform in 'unfocused' state

    Robdog,

    I managed to develop a workaround that has inperceptable flickering, but it means creating another userform:

    VB Code:
    1. Private Sub Workbook_Open()
    2.     OptionList.Show vbModeless
    3.     UserForm1.Show vbModeless           'dummy userform
    4.     Unload UserForm1
    5. End Sub

    I'm still interested in the API approach if you could post an example or a link. Thanks!

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Display Excel Userform in 'unfocused' state

    Ok, here it is. Just paste the code behind your userform and viola!
    VB Code:
    1. Option Explicit
    2. 'Code written by RobDog888
    3. 'Behind your userform:
    4. Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    5. ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    6.  
    7. Const SW_SHOWNORMAL    As Long = 1
    8. Const SW_SHOWMAXIMIZED As Long = 3
    9.  
    10. Const HWND_TOPMOST     As Long = -1
    11. Const HWND_NOTOPMOST   As Long = -2
    12. Const HWND_BOTTOM      As Long = 1
    13.  
    14. Const SWP_NOSIZE       As Long = &H1
    15. Const SWP_NOMOVE       As Long = &H2
    16. Const SWP_NOACTIVATE   As Long = &H10
    17. Const SWP_SHOWWINDOW   As Long = &H40
    18.  
    19. Private mbFirst        As Boolean
    20.  
    21. Private Sub UserForm_Activate()
    22.     'Show the window if first time
    23.     If mbFirst = True Then
    24.         SetWindowPos Application.hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    25.     Else
    26.         mbFirst = False
    27.     End If
    28. End Sub
    29.  
    30. Private Sub UserForm_Initialize()
    31.     mbFirst = True
    32. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Display Excel Userform in 'unfocused' state

    RobDog,

    Brilliant! Thanks. I decided to condense things a bit:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    4. ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    5. Private mbFirst As Boolean
    6.  
    7. Private Sub UserForm_Activate()
    8.     'Show the window if first time
    9.     If mbFirst = True Then
    10.         SetWindowPos Application.hWnd, CLng(1), 0, 0, 0, 0, CLng(&H40) Or CLng(&H2) Or CLng(&H1)
    11.     Else
    12.         mbFirst = False
    13.     End If
    14. End Sub
    15.  
    16. Private Sub UserForm_Initialize()
    17.     mbFirst = True
    18. End Sub

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Display Excel Userform in 'unfocused' state

    Thanks but using the consts for the parameters are a better method as you dont have to remember what they mean.

    Also, if you just pass the consts with the "&" long designator and add the hex Ors its evevn shorter.

    VB Code:
    1. SetWindowPos Application.hWnd, 1&, 0, 0, 0, 0, 67&
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Display Excel Userform in 'unfocused' state

    Understood. Most appreciated!

Posting Permissions

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



Click Here to Expand Forum to Full Width