VBA to close windows explorer window

wigarth

Board Regular
Joined
Apr 16, 2016
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi!

I need to open windows explorer to access a file, and it would be nice to have a macro in that file to automatically close the explorer window (This is not Internet-explorer!)
I found plenty of macros to open explorer windows (Shell) but none wich will work to close it.

The path to the file is given so there is basically not nessescary to have a loop function to close all windows.

Anyone with a solution to this?

Best reggards
Wig - Norway
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This will close the first window it finds that uses the "CabinetWClass" class which is used by Explorer in Windows 10:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Public Sub CloseExplorer()

CloseByClass "CabinetWClass"

End Sub
Private Sub CloseByClass(className As String)

PostMessage FindWindow("CabinetWClass", vbNullString), WM_CLOSE, 0, 0

End Sub

WBD
 
Upvote 0
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Public Sub CloseExplorer()

CloseByClass "CabinetWClass"

End Sub
Private Sub CloseByClass(className As String)

PostMessage FindWindow("CabinetWClass", vbNullString), WM_CLOSE, 0, 0

End Sub

Thank you WBD your code works great. My question is, can this code be expanded/adapted to loop through all open folders and close them.

Poenankie
 
Upvote 0
Closing all explorer windows would be:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Public Sub CloseAllExplorerWindows()

Dim wh As Long

Do While True
    wh = FindWindow("CabinetWClass", vbNullString)
    If wh = 0 Then Exit Do
    PostMessage wh, WM_CLOSE, 0, 0
Loop

End Sub

WBD
 
Upvote 0
Closing all explorer windows would be:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Public Sub CloseAllExplorerWindows()

Dim wh As Long

Do While True
    wh = FindWindow("CabinetWClass", vbNullString)
    If wh = 0 Then Exit Do
    PostMessage wh, WM_CLOSE, 0, 0
Loop

End Sub

WBD

Hi WBD

I don't think this is the right way, but new to the forum so trying this!
I've used an older post of yours to shrink text to fit an ActiveX Text Box, cheating through a label, it displays perfectly and I am running 24 of these on a page using your code. The problem is when I try and print it comes up with run time error 1004, unable to get the width property of the OLEObject Class
I'm sure it is something simple but it is doing my head in!

Thanks, hopefully
Paul
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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