title: Displaying an Excel userform as a button in the taskbar
body: <p>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:</p>
  
  <pre><code>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 = &amp;H40000
  Private Const WS_SYSMENU As Long = &amp;H80000
  Private Const WS_MINIMIZEBOX As Long = &amp;H20000
  Private Const WS_MAXIMIZEBOX As Long = &amp;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
  </code></pre>
  
  <p>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.</p>