Office: Direktbereich löschen

Gepostet am: Feb 26, 2011 10:16:49 PM

Ich verwende beim Entwickeln von Modulen oftmals den Direktbereich mittels > Debug.Print um den Zustand von Variablen udgl. auszugeben. Das Problem dabei ist, dass dieser nicht so einfach gelöscht werden kann. Bis dato habe ich meist den zu löschenden Bereich im Direktbereichfenster markiert und gelöscht. Es gibt jedoch auch eine einfachere Lösung - nämlich mit VBA. 

Direktbereich / ImmediateWindow löschen

Option ExplicitPrivate Declare Function GetWindow Lib "user32" ( _    ByVal hWnd As Long, _    ByVal wCmd As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _    ByVal lpClassName As String, _    ByVal lpWindowName As String) As LongPrivate Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _    ByVal hWnd1 As Long, _    ByVal hWnd2 As Long, _    ByVal lpsz1 As String, _    ByVal lpsz2 As String) As LongPrivate Declare Function GetKeyboardState Lib "user32" ( _    pbKeyState As Byte) As LongPrivate Declare Function SetKeyboardState Lib "user32" ( _    lppbKeyState As Byte) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _    ByVal hWnd As Long, _    ByVal wMsg As Long, _    ByVal wParam As Long, _    ByVal lParam As Long) As LongPrivate Const WM_KEYDOWN As Long = &H100Private Const KEYSTATE_KEYDOWN As Long = &H80Private savState(0 To 255) As BytePublic Sub ClearImmediateWindow()Dim hPane As LongDim tmpState(0 To 255) As Byte     hPane = GetImmHandle    If hPane = 0 Then MsgBox "Immediate Window not found."    If hPane < 1 Then Exit Sub     'Save the keyboardstate    GetKeyboardState savState(0)     'Sink the CTRL (note we work with the empty tmpState)    tmpState(vbKeyControl) = KEYSTATE_KEYDOWN    SetKeyboardState tmpState(0)    'Send CTRL+End    PostMessage hPane, WM_KEYDOWN, vbKeyEnd, 0&    'Sink the SHIFT    tmpState(vbKeyShift) = KEYSTATE_KEYDOWN    SetKeyboardState tmpState(0)    'Send CTRLSHIFT+Home and CTRLSHIFT+BackSpace    PostMessage hPane, WM_KEYDOWN, vbKeyHome, 0&    PostMessage hPane, WM_KEYDOWN, vbKeyBack, 0&     'Schedule cleanup code to run    Application.OnTime Now + TimeSerial(0, 0, 0), "DoCleanUp"End SubPrivate Sub DoCleanUp()    ' Restore keyboard state    SetKeyboardState savState(0)End SubPrivate Function GetImmHandle() As Long'This function finds the Immediate Pane and returns a handle.'Docked or MDI, Desked or Floating, Visible or HiddenDim oWnd As Object, bDock As Boolean, bShow As BooleanDim sMain$, sDock$, sPane$ Dim lMain&, lDock&, lPane&     On Error Resume Next    sMain = Application.VBE.MainWindow.Caption    If Err <> 0 Then        MsgBox "No Access to Visual Basic Project"        GetImmHandle = -1        Exit Function    End If     For Each oWnd In Application.VBE.Windows         If oWnd.Type = 5 Then             bShow = oWnd.Visible            sPane = oWnd.Caption            If Not oWnd.LinkedWindowFrame Is Nothing Then                bDock = True                sDock = oWnd.LinkedWindowFrame.Caption            End If            Exit For        End If    Next     lMain = FindWindow("wndclass_desked_gsk", sMain)    If bDock Then        'Docked within the VBE        lPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)        If lPane = 0 Then            'Floating Pane.. which MAY have it's own frame            lDock = FindWindow("VbFloatingPalette", vbNullString)            lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)            While lDock > 0 And lPane = 0                lDock = GetWindow(lDock, 2) 'GW_HWNDNEXT = 2                lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)            Wend        End If    ElseIf bShow Then        lDock = FindWindowEx(lMain, 0&, "MDIClient", vbNullString)        lDock = FindWindowEx(lDock, 0&, "DockingView", vbNullString)        lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)    Else        lPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)    End If     GetImmHandle = lPane  End Function

Diese Funktion hat jedoch den Nachteil, dass sie programmgesteuerte Zugriffe auf das Visual Basic Projekt erfordert, da es sonst zur Fehlermeldung "Laufzeitfehler '1004': Der programmatische Zugriff auf das Visual Basic-Projekt ist nicht sicher." kommt. Wie man dies abstellen kann erfährt man hier.

Alternativ kann man sich die kostenlosen MZ-Tools für VBA besorgen, die stellen eine entsprechende Schaltfläche für das Löschen des Direktbereiches in einer Symbolleiste zur Verfügung.

Links