Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an Excel userform as a button in the taskbar

Tags:

excel

vba

I want to hide the Excel button in the taskbar and display a separate button for my userform so that it feels like an application on its own. I know this has been covered a lot but I am having trouble with a specific issue: my code works fine when I step through it, but not if I let it run normally. Here is the code, which I have placed in the class module of Userform1:

Option Explicit

Private Declare Function GetWindowLong _
    Lib "user32" _
        Alias "GetWindowLongA" ( _
            ByVal hWnd As Long, _
            ByVal nIndex As Long) _
As Long

Private Declare Function SetWindowLong _
    Lib "user32" _
        Alias "SetWindowLongA" ( _
            ByVal hWnd As Long, _
            ByVal nIndex As Long, _
            ByVal dwNewLong As Long) _
As Long

Private Declare Function DrawMenuBar _
    Lib "user32" ( _
        ByVal hWnd As Long) _
As Long

Private Declare Function FindWindowA _
    Lib "user32" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) _
As Long

Private Const GWL_EXSTYLE = (-20)
Private Const GWL_STYLE As Long = (-16)
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_SYSMENU As Long = &H80000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000

Private Sub UserForm_Activate()

Dim lFrmWndHdl As Long
Dim lStyle As Long

lFrmWndHdl = FindWindowA(vbNullString, Me.Caption)
lStyle = GetWindowLong(lFrmWndHdl, GWL_STYLE)
lStyle = lStyle Or WS_SYSMENU
lStyle = lStyle Or WS_MINIMIZEBOX
lStyle = lStyle Or WS_MAXIMIZEBOX
SetWindowLong lFrmWndHdl, GWL_STYLE, (lStyle)
lStyle = GetWindowLong(lFrmWndHdl, GWL_EXSTYLE)
lStyle = lStyle Or WS_EX_APPWINDOW
SetWindowLong lFrmWndHdl, GWL_EXSTYLE, lStyle
DrawMenuBar lFrmWndHdl
AppActivate ("Microsoft Excel")
ThisWorkbook.Application.Visible = False

End Sub

Stepping through the code, when I step into the 2nd to last line AppActivate a separate button appears in the taskbar, and the last line hides the original button for the Excel workbook in the taskbar. I am then left with just a userform that can be maximised or minimised to the taskbar like any normal application. The problem is if I load the userform via code the separate button for the userform does not appear in the taskbar, so there are no Excel buttons left showing in the taskbar.

like image 267
Excel Developers Avatar asked Apr 11 '13 12:04

Excel Developers


People also ask

How do I open a UserForm in Excel without opening it?

To open the VBA window, click on Developer Tab and under Code Group, click on Visual Basic Button. Alternatively, you can also press short cut ALT + F11 to open the VBA window.


1 Answers

To answer my own question: the problem was not in the code I posted above, but in the way the userform was loaded. It should be loaded as modeless.

like image 185
Excel Developers Avatar answered Oct 02 '22 01:10

Excel Developers