Forumbeitrag
Excel-Version des Fragestellers:
2022
Erfahrungslevel des Fragestellers:
Excel gut - VBA bescheiden
Hi,
die Routine muss so aussehen wie Volti schon schrieb, bei mir heißt sie Maximieren, da sie als
Max die excel-eigene Funktion überschreibt.
Wenn du den Code aus meiner Datei übernimmst, dann maximiert es auch aus der Taskleiste.
Modul
Option Explicit
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, lpRect As RECT) As Long
Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
#If Win64 Then
Private Declare PtrSafe Function GetWindowLongA Lib "user32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongA Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#End If
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const GWL_STYLE As Long = (-16)
Private Const SWP_MYSTYLE As Long = &H224 ' SWP_NOZORDER + SWP_FRAMECHANGED + SWP_NOREPOSITION
Private Const WS_MYSTYLE As Long = &HCB0000 ' WS_CAPTION + WS_SYSMENU + WS_MAXIMINIMIZEBOX
Public bStatus As Boolean
Private Sub AusEin1(ByVal bWie As Boolean)
Dim hStil As LongPtr, R As RECT
With Application
GetWindowRect .hWnd, R
If bWie Then
.ExecuteExcel4Macro "Show.Toolbar(""Ribbon"", true)"
Else
.ExecuteExcel4Macro "Show.Toolbar(""Ribbon"", false)"
End If
.DisplayStatusBar = bWie ' Statusbar ein/aus
.DisplayFormulaBar = bWie ' Eingabefeld ein/aus
With ActiveWindow
.DisplayWorkbookTabs = bWie ' Register ein/aus
.DisplayHeadings = bWie ' Zeilen/Spaltenbezeichnungen ein/aus
.DisplayHorizontalScrollBar = bWie ' Scrollleiste ein/aus
.DisplayVerticalScrollBar = bWie ' Scrollleiste ein/aus
End With
hStil = GetWindowLongA(.hWnd, GWL_STYLE)
SetWindowLongA .hWnd, GWL_STYLE, IIf(bWie, hStil Or WS_MYSTYLE, hStil And Not WS_MYSTYLE)
SetWindowPos .hWnd, 0, R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top, SWP_MYSTYLE
End With
End Sub
Public Sub AllesAus()
bStatus = False
Call AusEin2(bStatus)
End Sub
Public Sub AllesEin()
bStatus = True
Call AusEin2(bStatus)
End Sub
Private Sub AusEin2(ByVal bWie As Boolean)
' Alternative mit gleichen Funktionen
Dim hStil As LongPtr, R As RECT
With Application
GetWindowRect .hWnd, R
.DisplayFullScreen = Not bWie ' Vollbild ein/aus
.DisplayStatusBar = bWie ' Statusbar ein/aus
.DisplayFormulaBar = bWie ' Eingabefeld ein/aus
With ActiveWindow
.DisplayWorkbookTabs = bWie ' Register ein/aus
.DisplayHeadings = bWie ' Zeilen/Spaltenbezeichnungen ein/aus
.DisplayHorizontalScrollBar = bWie ' Scrollleiste ein/aus
.DisplayVerticalScrollBar = bWie ' Scrollleiste ein/aus
End With
hStil = GetWindowLongA(.hWnd, GWL_STYLE)
SetWindowLongA .hWnd, GWL_STYLE, IIf(bWie, hStil Or WS_MYSTYLE, hStil And Not WS_MYSTYLE)
SetWindowPos .hWnd, 0, R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top, SWP_MYSTYLE
End With
End Sub
Sub Minimieren()
Application.WindowState = xlMinimized
Application.ActiveWindow.OnWindow = "Maximieren"
End Sub
'Die Max-Routine, UMBENENNEN
Sub Maximieren()
Application.WindowState = xlMaximized 'DAS IST FALSCH: xlNormal
If bStatus Then
Call AllesEin
Else
Call AllesAus
End If
End Sub
Arbeitsmappe
Private Sub Workbook_WindowResize(ByVal Wn As Window)
Dim myApp As Excel.Application
Set myApp = Wn.Application
If myApp.WindowState = -4137 And bStatus = False Then
Call Maximieren
'CALL Max ggf.
End If
End Sub
hth
Ulf