Check If Folder Is Open and or open it?

mayoung

Active Member
Joined
Mar 26, 2014
Messages
259
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Looking for code to see if a folder is open if it is maximize it? If its not Open open it?
 
I am not sure what to do with that or where to put it. I am a novice at this. I am sure you tell.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Ok - start again :eeek:

Paste everything below into a new module and run OpenFolderDemo
Code:
Private Const SW_RESTORE = 9

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  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
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    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
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  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 = "C:\Windows"
Call OpenFolder(strPath)
End Sub
 
Upvote 0
I tried putting that code in that same location before I left for work this morning and it did not work. After reading the code you posted I noticed I copied over a comment line that was above the code in the article. I removed the comment line and it works perfect.
Thank You!!
 
Upvote 0
I'm really glad the code still works for you all! ;) If you notice any bugs, let me know and I'll work to resolve them.
 
Upvote 0
Actually it worked perfect the first time I ran it. But then when I went back to run it it stopped working for some reason? The code runs and does not error but nothing happens. I put another folder path in the openfiledemo code and it ran perfect but only once..It like it works sporadically?
 
Upvote 0
Wellsr what I doing in a workbook I have different forms control buttons going to different folders. I was able able to bounce between folders ok but then it locked up. Any suggestions?
 
Upvote 0
Hi, mayoung,

Is the folder minimized each it doesn't come back to the foreground? Or is it also behaving sporadically even if the the folder is just buried behind another window (but not actually minimized)?
 
Upvote 0
Greetings Wellsr,

I am running windows 10 Home Edition 64 bit-operating system. With Office 365.

I am testing the code with two modules and each module contains the same code but with a 1 after the one sub name so there not named the same. In each module the code has a different strPath. I have two form contol buttons one linked to each code. When you switch betweens buttons the code may work or it may not when switching between buttons. Its like there locked up. Somestimes I can change the strPath to different folders save, close and reopen and it may or may not work.

Any help would be appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top