Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1916to1920
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Minimiertes Explorer Fenster anzeigen

Minimiertes Explorer Fenster anzeigen
30.01.2023 15:50:55
blutiger
Hallo liebes Forum,
ich habe jetzt schon stundenlang recherchiert, aber nicht selbst zu einer Lösung gekommen. Vermutlich ist es aber ganz einfach und ich bin nur zu blöd.
Falls ein bestimmter Pfad im Explorer (der "normale", nicht der Internet Explorer) NICHT geöffnet ist, soll der Explorer unter diesem Pfad geöffnet werden. Das habe ich schon hinbekommen. Aber Folgendes klappt noch nicht: Wenn der Explorer schon unter diesem Pfad geöffnet ist, soll er im Vordergrund angezeigt werden (für den Fall, dass er im Hintergrund ist und/oder minimiert ist). Bisher klappt es nur, dass das Fenster in den Vordergrund kommt (mit win.Activate), wenn es hinter einem anderen Fenster "versteckt" ist. Aber wenn das Fenster minimiert ist, klappt das nicht.
Im Folgenden mein Code, den Pfad hab ich aus Datenschutzgründen in diesem Forum in "Beispielpfad" geändert.
Sub explorer_anzeigen()
    Dim objShell As Object, win As Object
    Dim strFullPath As String, gefunden As Boolean
    strFullPath = "Beispielpfad\vorlagen" 'Pfad anpassen
    gefunden = False
    If Dir(strFullPath, vbDirectory) > "" Then
      Set objShell = CreateObject("Shell.Application")
      For Each win In objShell.Windows
        If InStr(1, UCase(win.FullName), "EXPLORER") > 0 Then
          If LCase(win.Document.Folder.self.Path) = LCase(strFullPath) Then
            AppActivate win.locationname
            win.Activate
            gefunden = True
            Exit For
          End If
        End If
      Next
      If gefunden = False Then objShell.explore (strFullPath)
      Set objShell = Nothing
    Else
      MsgBox "Pfad nicht vorhanden!", vbCritical
    End If
End Sub
Ich hab das Gefühlt, dass die Lösung wieder ganz einfach ist, aber ich krieg es trotz mehrstündiger Recherche einfach nicht hin.
Schonmal danke für die Antworten! :)

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Minimiertes Explorer Fenster anzeigen
30.01.2023 16:44:06
Nepumuk
Hallo,
teste mal:
Option Explicit
Private Declare PtrSafe Function EnumWindows Lib "user32.dll" ( _
    ByVal lpEnumFunc As LongPtr, _
    ByVal lParam As LongPtr) As Long
Private Declare PtrSafe Function GetWindowTextA Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLengthA Lib "user32.dll" ( _
    ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function GetClassNameA Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpClassName As String, _
    ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetWindowLongPtrA Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByRef lpdwProcessId As Long) As Long
Private Declare PtrSafe Function AllowSetForegroundWindow Lib "user32.dll" ( _
    ByVal dwProcessId As Long) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" ( _
    ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal nCmdShow As Long) As Long
Private Const SW_SHOWNORMAL As Long = 1
Private Const GWL_STYLE As Long = -16
Private Const WS_VISIBLE As Long = &H10000000
Private Const WS_BORDER As Long = &H800000
Private Const GC_CLASSNAME_EXPLORER As String = "CabinetWClass"
Private Const FOLDER_PATH As String = "H:\230125" 'Pfad anpassen
Private lblnFound As Boolean
Private llngprtHwnd As LongPtr
Public Sub explorer_anzeigen()
    Dim objShell As Object
    Dim lngProcessID As Long
    If Dir$(FOLDER_PATH, vbDirectory) > vbNullString Then
        lblnFound = False
        Call EnumWindows(AddressOf WindowCallBack, ByVal 0)
        If lblnFound Then
            lngProcessID = GetWindowThreadProcessId(llngprtHwnd, ByVal 0&)
            Call AllowSetForegroundWindow(lngProcessID)
            Call SetForegroundWindow(llngprtHwnd)
            Call ShowWindow(llngprtHwnd, SW_SHOWNORMAL)
        Else
            Set objShell = CreateObject("Shell.Application")
            Call objShell.Explore(FOLDER_PATH)
            Set objShell = Nothing
        End If
    Else
        MsgBox "Pfad nicht vorhanden!", vbCritical
    End If
End Sub
Private Function WindowCallBack(ByVal lngptrHwnd As LongPtr, ByVal lngptrParam As LongPtr) As LongPtr
    Dim strCaption As String, strClassName As String
    Dim lngReturn As Long, lngptrStyle As LongPtr
    lngptrStyle = GetWindowLongPtrA(lngptrHwnd, GWL_STYLE)
    If lngptrStyle And (WS_VISIBLE Or WS_BORDER) = WS_VISIBLE Or WS_BORDER Then
        strClassName = Space$(256)
        lngReturn = GetClassNameA(lngptrHwnd, strClassName, 256)
        strClassName = Left$(strClassName, lngReturn)
        If strClassName = GC_CLASSNAME_EXPLORER Then
            lngReturn = GetWindowTextLengthA(lngptrHwnd)
            strCaption = Space$(lngReturn)
            Call GetWindowTextA(lngptrHwnd, strCaption, lngReturn + 1)
            If strCaption = FOLDER_PATH Then
                llngprtHwnd = lngptrHwnd
                lblnFound = True
                Exit Function
            End If
        End If
    End If
    WindowCallBack = 1
End Function
Gruß
Nepumuk
Anzeige
AW: Minimiertes Explorer Fenster anzeigen
02.02.2023 17:18:11
blutiger
Danke, Nepumuk, für deine ausführliche Antwort. Leider kommt dann bei mir die Meldung "Fehler beim Kompilieren: Nach End Sub, End Function oder End Property können nur Kommentare stehen.
AW: Minimiertes Explorer Fenster anzeigen
02.02.2023 17:39:48
Nepumuk
Hallo,
das kommt nicht von meinem Code, der ist getestet. Füge ihn mal in ein neues Modul ein.
Gruß
Nepumuk

34 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige