Solved Closing specific folders using VBA (1 Viewer)

jazsriel

Member
Local time
Today, 07:24
Joined
Dec 21, 2020
Messages
62
Hi, been awhile since I was on here. Right now there I am on the last part of my current project and I have no idea how to solve my problem. I have a specific Windows folder that I need to close when a button is clicked on a form. I have 0 code on how to accomplish this.

Basically, a blank form with a single button on it. Click the button and a specific folder that was previously opened will close.

Any ideas to point me in the right direction. I am completely lost on this.
 

Jason Lee Hayes

Active member
Local time
Today, 13:24
Joined
Jul 25, 2020
Messages
175
 

jazsriel

Member
Local time
Today, 07:24
Joined
Dec 21, 2020
Messages
62
Thank you but this it appears the link you provided is referring to using shell commands. To my knowledge, which is limited these commands won't work in VBA, but I might be wrong. Please let me know if I am wrong.

But thank you for the reply either way.
 

Jason Lee Hayes

Active member
Local time
Today, 13:24
Joined
Jul 25, 2020
Messages
175
Thank you but this it appears the link you provided is referring to using shell commands. To my knowledge, which is limited these commands won't work in VBA, but I might be wrong. Please let me know if I am wrong.

But thank you for the reply either way.
You will need to use API I guess, don't think FSO can do this as your opening a folder in Windows Explorer therefore you will need to identify the correct open window and reference it before you can close it. I can only do it with API and utilising the shell command sorry. Likely someone on here can achieve it via vba only but that's not me lol
 

jazsriel

Member
Local time
Today, 07:24
Joined
Dec 21, 2020
Messages
62
I have tried to go back again today and reference the information you provided in the attached link, however my company has restricted access to the vbforums site. I do not have microsoft products at home, so I am unable to really do much from there. So right now, I am at a complete stand still. I even tried to use a batch program which was able to successfully close the folder, but only if the folders and the batch command were under my personal user account login. Once they were moved to the public directory, and the batch file adjusted accordingly they stopped working.
 
Last edited:

olxx

Registered User.
Local time
Today, 05:24
Joined
Oct 2, 2009
Messages
55
I assume that the explorer window you want to close is identified by folder path. In this case this code closes the explorer window you want. Edit thetargetFolderPath part in quotes.

Code:
Sub CloseExplorerWindowByPath()
    Dim shellWindows As Object
    Dim explorerWindow As Object
    Dim targetFolderPath As String

    ' Set the target folder path
    targetFolderPath = "C:\yourpath"

    ' Create ShellWindows object
    Set shellWindows = CreateObject("Shell.Application").Windows

    ' Loop through each Explorer window
    For Each explorerWindow In shellWindows
        ' Check if the window's document folder path matches the target path
        If InStr(1, explorerWindow.Document.Folder.Self.Path, targetFolderPath, vbTextCompare) > 0 Then
            ' Close the matching window
            explorerWindow.Quit
            Exit For
        End If
    Next explorerWindow
End Sub
 

jazsriel

Member
Local time
Today, 07:24
Joined
Dec 21, 2020
Messages
62
I assume that the explorer window you want to close is identified by folder path. In this case this code closes the explorer window you want. Edit thetargetFolderPath part in quotes.

Code:
Sub CloseExplorerWindowByPath()
    Dim shellWindows As Object
    Dim explorerWindow As Object
    Dim targetFolderPath As String

    ' Set the target folder path
    targetFolderPath = "C:\yourpath"

    ' Create ShellWindows object
    Set shellWindows = CreateObject("Shell.Application").Windows

    ' Loop through each Explorer window
    For Each explorerWindow In shellWindows
        ' Check if the window's document folder path matches the target path
        If InStr(1, explorerWindow.Document.Folder.Self.Path, targetFolderPath, vbTextCompare) > 0 Then
            ' Close the matching window
            explorerWindow.Quit
            Exit For
        End If
    Next explorerWindow
End Sub
Yes, you are correct that the folder I needed to close is identified by path. This also solves the issue I was having. Thank you. I will mark as solved.
 

Users who are viewing this thread

Top Bottom