Map openen of activeren in explorer

Status
Niet open voor verdere reacties.

Vandebeyer

Nieuwe gebruiker
Lid geworden
20 jun 2019
Berichten
4
Ik zou graag het volgende willen:

De map openen waar het actieve bestand opgeslagen staat.
Dat doe ik met de volgende code, en werkt prima.

Call Shell("explorer.exe " & ThisWorkbook.Path, vbNormalFocus)

Nu zou ik graag een controle slag uitvoeren om te kijken of deze map al open is.
Want dan zou ik hem graag activeren in plaats van openen. Ik heb nu vaak 10 keer dezelfde map open staan.
Wie kan mij helpen?
 
Laatst bewerkt:
Die had ik ook al gevonden maar krijg ik niet aan de gang.



'--------------------------------------------
Private Const SW_RESTORE = 9

#If VBA7 Then
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare PtrSafe Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#Else
' Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
' Private Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If

Private Sub OpenFolder(strDirectory As String)
'DESCRIPTION: Open folder if not already open. Otherwise, activate the already opened window
'DEVELOPER: Ryan Wells (wellsr.com)
'INPUT: Pass the procedure a string representing the directory you want to open
Dim pID As Variant
Dim sh As Variant
On Error GoTo 102:
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Then
If w.document.Folder.self.Path = strDirectory Then
'if already open, bring it front
If CBool(IsIconic(w.hwnd)) Then ' If it's minimized, show it
w.Visible = False
w.Visible = True
ShowWindow w.hwnd, SW_RESTORE
Else
w.Visible = False
w.Visible = True
End If
Exit Sub
End If
End If
Next
'if you get here, the folder isn't open so open it
pID = Shell("explorer.exe " & strDirectory, vbNormalFocus)
102:
End Sub




Sub OpenFolderDemo()
'Demo - opens the folder location saved to the variable strPath
Dim strPath As String
strPath = ActiveWorkbook.Path
Call OpenFolder(strPath)
End Sub





In het eerste stuk achter # Else heb ik die twee regels uitgezet. Dan wordt het in ieder geval uitgevoerd. Maar dat zal wel met de versie van VB te maken hebben.

Hij bij maar gewoon telkens de map opnieuw openen.

Zit het dan misschien in de volgende regel:

If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Then

Misschien moet die naam anders zijn als de windows versie nederlands is?
Met Windows Verkenner werkt het overigens ook niet.
 
Laatst bewerkt:
Via de code zelf kan je controleren wat w.Name is als er al een folder open is.
Dat zal dan Verkenner zijn. Pas daar dan de controle op aan:
Code:
If w.Name = "Windows Explorer" Or w.Name = "File Explorer" [COLOR="#FF0000"]Or w.Name = "Verkenner"[/COLOR] Then

In de originele versie, niet aangepaste versie dus.
 
Laatst bewerkt:
Code:
'--------------------------------------------
Private Const SW_RESTORE = 9

#If VBA7 Then
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare PtrSafe Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#Else
 '   Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
 '   Private Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If

Private Sub OpenFolder(strDirectory As String)
'DESCRIPTION: Open folder if not already open. Otherwise, activate the already opened window
'DEVELOPER: Ryan Wells (wellsr.com)
'INPUT: Pass the procedure a string representing the directory you want to open
Dim pID As Variant
Dim sh As Variant
On Error GoTo 102:
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
    If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Or w.Name = "Verkenner" Then
        If w.document.Folder.self.Path = strDirectory Then
            'if already open, bring it front
            If CBool(IsIconic(w.hwnd)) Then ' If it's minimized, show it
                w.Visible = False
                w.Visible = True
                ShowWindow w.hwnd, SW_RESTORE
            Else
                w.Visible = False
                w.Visible = True
            End If
            Exit Sub
        End If
    End If
Next
'if you get here, the folder isn't open so open it
pID = Shell("explorer.exe " & strDirectory, vbNormalFocus)
102:
End Sub




Sub OpenFolderDemo()
'Demo - opens the folder location saved to the variable strPath
Dim strPath As String
strPath = ActiveWorkbook.Path
Call OpenFolder(strPath)
End Sub

Zo bedoel je?

Volgens mij zou dit dan moeten werken, maar hij blijft ze naast elkaar openen.
 
Werkt hier prima.
Je hebt wel gecontroleerd of "Verkenner" correct is?
Code:
For Each w In sh.Windows
    MsgBox w.name
 
Werkt!!

Inderdaad de naam "Verkenner", ik gokte op "Windows Verkenner" omdat die in de taakbeheer zo genoemd wordt.

MsgBox w.name is daarvoor een handig tooltje :thumb:


Top, bedankt voor de snelle assistentie!
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan