2013-04-11 71 views
5

我想隱藏任務欄中的Excel按鈕,併爲我的用戶窗體顯示一個單獨的按鈕,以使其感覺像是一個應用程序。我知道這已經被覆蓋了很多,但是我遇到了一個特定問題:我的代碼在我單步執行時工作正常,但是如果我讓它正常運行,則不會。下面是代碼,我已經放置Userform1的類模塊中:將Excel用戶窗體顯示爲任務欄中的按鈕

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 

通過代碼步進,當我步入第二到最後一行則AppActivate一個單獨的按鈕顯示在任務欄,最後一行在任務欄中隱藏Excel工作簿的原始按鈕。然後,我只剩下一個用戶窗體,它可以像任何常規應用程序一樣最大化或最小化到任務欄。問題是如果我通過代碼加載用戶窗體,用戶窗體的單獨按鈕沒有出現在任務欄中,所以任務欄中沒有顯示任何Excel按鈕。

+0

如果加上'DoEvents'後'DrawMenuBar lFrmWndHdl' – 2013-04-11 13:24:17

+0

而且還'則AppActivate( 「Microsoft Excel中」)後會發生什麼' – 2013-04-11 13:30:32

+0

我已經試過了。它沒有幫助。我也嘗試了application.wait。 – 2013-04-11 13:32:25

回答

1

要回答我自己的問題:問題不在我上面發佈的代碼中,而是以用戶窗體加載的方式。它應該作爲無模式加載。

0

只需將下面的代碼放在您的主UserForm_Initialize子文件中,它將最小化Excel應用程序窗口,同時使您的表單在桌面上保持打開狀態。

Private Sub minimizeWindow() 
    With Application 
    .WindowState = xlMinimized 
    End With 
End sub 
+0

這仍將在任務欄中顯示Excel應用程序。我想隱藏它。 – 2017-01-11 15:35:00

相關問題