2017-03-06 1888 views
0

我有一個用戶窗體在打開工作簿時打開。 Excel也是隱藏的,這樣用戶窗體就會顯示給用戶。vba在打開時顯示用戶窗體,隱藏工作表,但保留任務欄圖標

Private Sub Workbook_Open() 
Application.Visible = False 
UserForm1.Show vbModeless 
End Sub 

然而,這也隱藏了Excel的圖標在任務欄上,這樣當用戶從用戶窗體中點擊了他們不能回,除非使用Alt + Tab或關閉/最小化其他窗口是在前面的用戶表單。我不希望用戶這樣做,有些甚至可能會嘗試再次打開表單(假定它已關閉),導致重新打開的提示和錯誤,我也不想這樣做。

本質上,我需要用戶窗體的任務欄上的圖標。

一旦用戶窗體關閉我有它,以便Excel關閉

Unload UserForm1 
Application.Quit 

例子我已經在網上對於這個問題上找到不太達到什麼,我試圖做的。 改變形式,以儘量減少和開放的模式工作,以保持圖標在任務欄,而不是讓用戶編輯工作表

Application.WindowState = xlMinimized 
UserForm1.Show (1) 

但是,這裏有2個問題.....月1日 - 用戶窗體不成爲焦點,第二 - 用戶可以點擊任務欄圖標,工作表現在可以在用戶窗體後面看到,這不是我能夠做到的。

+1

這是怎麼回事:http://stackoverflow.com/questions/15949023/displaying-an-excel-userform-as-a-button-in-the-taskbar –

+0

我剛剛試過,它沒有出現做任何事。雖然我對類模塊不熟悉,但它的實現可能有些問題。我將它粘貼到用戶窗體的新類模塊中(右鍵單擊>插入>類模塊),保存並再次運行工作簿。 – Aurelius

+0

我明白,問題中的解決方案不是直截了當的,但非常*看起來像*重複您的問題。該問題中的代碼出現在Userform模塊中 - 實際上它是一個Class。 –

回答

0

而不是隱藏應用程序最小化工作簿:

ThisWorkbook.Windows(1).WindowState = xlMinimized 
+0

行發生錯誤「無效的過程調用或參數」,我應該包含這個,因爲我已經有了。問題是用戶可以單擊任務欄圖標再次最大化工作表 – Aurelius

0

我花的時間量在Excel-的Visio應用程序的開發這一任務,並面臨着同樣的問題(Excel表格以上的Visio/Excel和VBA編輯器是隱藏的 - 但用戶很容易失去焦點,只能退回 - Alt-Tabbing)。同樣的問題!

我的算法來解決這個問題是這樣的(在用戶窗體類中的所有代碼):

Private Sub UserForm_Initialize() 

    'some init's above 
    ToggleExcel   'Toggle excel, all windows are hidden now! 
    ActivateVisio  'Visio fired and on top 
    SetStandAloneForm 'Let's customize form 
End Sub 

所以在啓動時,我們有我們所期望的Visio和形式。再次在Terminate事件I ToggleExcel並儘量減少Visio。

ToggleExcel

Private Function ToggleExcel() 
    Static IsVBEWasVisible As Boolean 

    With Application 
     If .Visible = True Then 
      IsVBEWasVisible = .VBE.MainWindow.Visible 
      If IsVBEWasVisible Then _ 
        .VBE.MainWindow.Visible = False 
      .WindowState = xlMinimized 
      .Visible = False 
     Else 
      If IsVBEWasVisible Then _ 
        .VBE.MainWindow.Visible = True 
      .WindowState = xlMaximized 
      .Visible = True 
     End If 
    End With 
End Function 

SetStandAloneForm

要SetStandAloneForm我聲明API函數的塊:

#If VBA7 Then 
    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long 
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
    Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 
    Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long 
#Else 
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 
    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long 
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 SetFocus Lib "user32" (ByVal hWnd As Long) As Long 
#End If 

實際SetStandAloneForm:

Private Function SetStandAloneForm() 
    Const GWL_STYLE As Long = -16 
    Const GWL_EXSTYLE As Long = -20 
    Const WS_CAPTION As Long = &HC00000 
    Const WS_MINIMIZEBOX As Long = &H20000 
    Const WS_MAXIMIZEBOX As Long = &H10000 
    Const WS_POPUP As Long = &H80000000 
    Const WS_VISIBLE As Long = &H10000000 
    Const WS_EX_DLGMODALFRAME As Long = &H1 
    Const WS_EX_APPWINDOW As Long = &H40000 
    Const SW_SHOW As Long = 5 

    Dim Hwnd As Long 
    Dim CurrentStyle As Long 
    Dim NewStyle As Long 

    If Val(Application.Version) < 9 Then 
     Hwnd = FindWindow("ThunderXFrame", Me.Caption) 'XL97 
    Else 
     Hwnd = FindWindow("ThunderDFrame", Me.Caption) '>XL97 
    End If 

    'Let's give to userform minimise and maximise buttons 
    CurrentStyle = GetWindowLong(Hwnd, GWL_STYLE) 
    NewStyle = CurrentStyle Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX 
    NewStyle = NewStyle And Not WS_VISIBLE And Not WS_POPUP 
    Call SetWindowLong(Hwnd, GWL_STYLE, NewStyle) 

    'Let's give to userform a taskbar icon 
    CurrentStyle = GetWindowLong(Hwnd, GWL_EXSTYLE) 
    NewStyle = CurrentStyle Or WS_EX_APPWINDOW 
    Call SetWindowLong(Hwnd, GWL_EXSTYLE, NewStyle) 
    Call ShowWindow(Hwnd, SW_SHOW) 

End Function 
+0

您的第三部分代碼(SetStandAloneForm)駐留在哪裏?當我把它放到Userform類 – Aurelius

+1

@Demo時,它給我帶來了一個錯誤,嘿,哪個錯誤?這個塊可以放在你想要的任何地方(但是在API函數的範圍內),你只需要在那裏傳遞'Caption'形式並替換裏面的'Me.Caption'部分。 – CommonSense

+0

我將它們放置在順序中,所以我在第3個代碼框中將第一個框中的代碼的'End Function'放在'Private Funtion ToggleExcel()'之前。錯誤是'編譯錯誤:只有註釋可能出現在結束小組,結束功能,結束屬性' – Aurelius

相關問題