2016-06-01 68 views
1

我希望我的excel xml始終以全屏視圖顯示。在excel中總是顯示全屏vba

爲此,我下一個代碼:

Private Sub Workbook_Open() 
      Application.WindowState = xlMaximized 
      ActiveWindow.WindowState = xlMaximized 
      Application.DisplayFullScreen = True 
    End Sub 

這是工作的罰款,直到我儘量減少Excel中,一旦我再次最大化它顯示在普通視圖模式下,如何進行?任何建議?主要想法是刪除工具欄,因爲我不希望用戶與它們交互。

+0

關於您的工作表在與最小化交互發生時恢復的點,最大化選項。我會嚴格建議你不要使用系統設置,但最新的編輯包括與windows API級別交互的相關代碼。 – skkakkar

回答

0

還有就是你可以陷阱我會嘗試加入這個您的ThisWorkbook模塊

Option Explicit 

Private mbToggle As Boolean 
Private mlPriorState(-1 To 0) As XlWindowState 


Private Sub Workbook_WindowResize(ByVal Wn As Window) 

    mlPriorState(mbToggle) = Wn.WindowState 
    mbToggle = Not mbToggle 

    If Wn.WindowState = xlNormal And mlPriorState(mbToggle) <> xlMaximized Then 
     ActiveWindow.WindowState = xlMaximized 
    End If 

End Sub 

雖然這可能僅在代表工作表/工作簿窗口工作的事件。我會先試試這個;涉及Windows API的其他解決方案更爲複雜。

摺疊在一些反饋。此代碼適用於我。

+0

感謝您的鼓勵。 – skkakkar

+0

當用戶想要最小化窗口時,如何防止窗口被重新鏡像?當我剛剛嘗試時,當我最小化窗口時,Application.Windowstate'返回了'xlNormal'。 – arcadeprecinct

0

Workbook_Activate將帶來全屏模式,而其他模式將恢復正常模式。

Private Sub Workbook_Activate() 
    On Error Resume Next 
    With Application 
     .DisplayFullScreen = True 
     .CommandBars("Worksheet Menu Bar").Enabled = False 
    End With 

End Sub 

Private Sub Workbook_Deactivate() 
    On Error Resume Next 
    With Application 
     .DisplayFullScreen = False 
     .CommandBars("Worksheet Menu Bar").Enabled = True 
    End With 

End Sub 

編輯
你不應該 '修改' Windows上運行在系統級的方法。但是,如果你真的,真的必須;將以下內容添加到新模塊並調用SetStyle過程。

這代碼提供UNTESTED「是」 - 的API是在系統級修改Windows和可能是危險的方式(突然崩潰,數據文件損壞......),如果你不知道什麼你在做。

VB:

Option Explicit 

'Related Windows API functions 
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 GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long 
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long 
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long 
Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long 
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWndLock As Long) As Long 

'Window style constants 
Private Const GWL_STYLE As Long = (-16) '// The offset of a window's style 
Private Const GWL_EXSTYLE As Long = (-20) '// The offset of a window's extended style 
Private Const WS_CAPTION As Long = &HC00000 '// Title bar bit 
Private Const WS_SYSMENU As Long = &H80000 '// System menu bit 
Private Const WS_THICKFRAME As Long = &H40000 '// Sizable frame bit 
Private Const WS_MINIMIZEBOX As Long = &H20000 '// Minimize box bit 
Private Const WS_MAXIMIZEBOX As Long = &H10000 '// Maximize box bit 
Private Const WS_EX_TOOLWINDOW As Long = &H80 '// Tool Window: small titlebar bit 

'Constant to identify the Close menu item 
Private Const SC_CLOSE As Long = &HF060 


Public Sub SetStyle() 

    Dim lStyle As Long, hMenu As Long 

    'Get the basic window style 
    lStyle = GetWindowLong(Application.hWnd, GWL_STYLE) 

    If lStyle = 0 Then 
     MsgBox "Unable to determine application window handle...", vbExclamation, "Error" 
     Exit Sub 
    End If 

    '// Build up the basic window style flags for the form 
    '// Uncomment the features you want... 
    '// Set it True to enable, FALSE to disable 
    '// The first 2 are obvious, ThickFrame controls if the Window is sizable or not. 

    '// SetBit lStyle, WS_CAPTION, True 
    '// SetBit lStyle, WS_SYSMENU, False 
    '// SetBit lStyle, WS_THICKFRAME, False 
    SetBit lStyle, WS_MINIMIZEBOX, False 
    SetBit lStyle, WS_MAXIMIZEBOX, False 

    'Set the basic window styles 
    SetWindowLong Application.hWnd, GWL_STYLE, lStyle 

    'Get the extended window style 
    lStyle = GetWindowLong(Application.hWnd, GWL_EXSTYLE) 


    '// Handle the close button differently 
    '// If Close button is wanted 
    '// hMenu = GetSystemMenu(Application.hWnd, 1) 

    '// Not wanted - delete it from the control menu 
    hMenu = GetSystemMenu(Application.hWnd, 0) 
    DeleteMenu hMenu, SC_CLOSE, 0& 


    'Update the window with the changes 
    DrawMenuBar Application.hWnd 
    SetFocus Application.hWnd 

End Sub 


'// Set or clear a bit from a style flag 
Private Sub SetBit(ByRef lStyle As Long, ByVal lBit As Long, ByVal bOn As Boolean) 

    If bOn Then 
     lStyle = lStyle Or lBit 
    Else 
     lStyle = lStyle And Not lBit 
    End If 

End Sub 
+0

好的@skkakkar –

+0

代碼工作正常,但它只適用於工作簿更改(最小化/最大化),它不適用於Excel應用程序最小化/最大化。作爲主要的海豚是用戶無法訪問工具欄主頁/插入/頁面佈局/公式/數據.....隱藏它的任何其他選項? –

+0

@Make Standard我有權訪問這些例程,但專家非常強烈地建議不要使用任何能夠減少或最大化按鈕的例程,因爲您可以永久丟失這些按鈕。 – skkakkar

1

粘貼到工作簿模塊此。它將在窗口被調整大小時使窗口最大化:

Private Sub Workbook_WindowResize(ByVal Wn As Window) 
    ActiveWindow.WindowState = xlMaximized 
End Sub