Check If Folder Is Open and or open it?

mayoung

Active Member
Joined
Mar 26, 2014
Messages
257
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?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I am confused by your question :confused:
Do you mean test to see if a specific workbook is open?

amend full path
Code:
Sub ActivateWorkbook()
    Const f = "[COLOR=#ff0000]C:\folder\subfolder\filename.xlsx[/COLOR]"

    On Error Resume Next
    Workbooks(Split(f, "\")(UBound(Split(f, "\")))).Activate
    If Err.Number > 0 Then Workbooks.Open Filename:=f
End Sub
 
Upvote 0
No just open a certain path to a folder. I have code to open a folder but each time it runs it opens another window..So unless I close the first occurrence I may have multiple windows open with the same file path..Does thus make sense?
 
Upvote 0
James006,
Not sure how to interpret the code. Where do you insert the folder path or folder name?
 
Upvote 0
Where do you insert the folder path or folder name?
Code:
<code class="language-vb" data-lang="vb">Sub OpenFolderDemo()
'Demo - opens the folder location saved to the variable strPath
    Dim strPath As String
    strPath = "[COLOR=#ff0000]C:\Windows[/COLOR]"
    Call OpenFolder(strPath)
End Sub</code>
 
Last edited:
Upvote 0
Ok this is the code I am trying per the article.
This line of code is thowing a error: If CBool(IsIconic(w.hwnd)) Then ' If it's minimized, show it

It's saying IsIconic (Sub of Function not defined?)

Any Ideas how to fix?

Thank You!

Code:
Sub OpenFolderDemo()'Demo - opens the folder location saved to the variable strPath
Dim strPath As String
strPath = "C:\Users\Mark\OneDrive\Documents\Excel\EverydayExcel"
Call OpenFolder(strPath)
End Sub


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
 
Upvote 0
Where is this in your code?

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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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