• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How To Close A Windows Folder Using VBA

Shell "explorer C:Windows" will open a folder.


But surprisingly I'm a little troubled finding something to close a folder.


I need to ensure a particular folder is closed before running a code.


I was going to add it to the beginning of code.
 
Hui!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


I thought you abandoned me : )


Good to hear from you again man. How's Australia life?


Anyway, you pointed me in the right direction.


This basically writes a batch file down and launches it to terminate all open folders.


It refreshes Windows basically.


Hope this helps someone out.


Sub Close_All_Windows_Folders()

'''''''''''''''''''''''''''''''''''''''''''''''''

Application.Calculation = xlCalculationManual

Application.ScreenUpdating = False

Application.EnableEvents = False

'''''''''''''''''''''''''''''''''''''''''''''''''

Dim BatchFileDataDirFolder As String

Dim BatchFileDataFileName As String

Dim CloseAllFoldersBatchFile As String

Dim FileNumber As Integer

Dim ExecuteBatchFile As Variant

'''''''''''''''''''''''''''''''''''''''''''''''''

BatchFileDataDirFolder = "C:BatchFileDataFolder" '<<<Change Directory Folder

BatchFileDataFileName = "Close.All.Folders.bat" '<<<Change BAT File Name

CloseAllFoldersBatchFile = BatchFileDataDirFolder & "" & BatchFileDataFileName

FileNumber = FreeFile

'''''''''''''''''''''''''''''''''''''''''''''''''

On Error Resume Next

Kill BatchFileDataDirFolder & "*.*"

On Error Resume Next

RmDir BatchFileDataDirFolder

'''''''''''''''''''''''''''''''''''''''''''''''''

MkDir BatchFileDataDirFolder

'''''''''''''''''''''''''''''''''''''''''''''''''

Open CloseAllFoldersBatchFile For Output As #FileNumber

Print #FileNumber, "taskkill /f /im explorer.exe"

Print #FileNumber, "start /d" & """" & "C:Windows" & """" & " EXPLORER.exe"

Print #FileNumber, "exit"

Close #FileNumber

'''''''''''''''''''''''''''''''''''''''''''''''''

ExecuteBatchFile = Shell(CloseAllFoldersBatchFile, vbHide)

'''''''''''''''''''''''''''''''''''''''''''''''''

'Error Assistance

If ExecuteBatchFile = 0 Then

MsgBox "Error : " & Err.Number & vbCrLf & "Description : " & Err.DESCRIPTION, vbCritical

Close #FileNumber

End

End If

'''''''''''''''''''''''''''''''''''''''''''''''''

Application.Wait (Now + TimeValue("0:00:01"))

On Error Resume Next

Kill BatchFileDataDirFolder & "*.*"

On Error Resume Next

RmDir BatchFileDataDirFolder

'''''''''''''''''''''''''''''''''''''''''''''''''

Application.EnableEvents = True

Application.ScreenUpdating = True

Application.Calculation = xlCalculationAutomatic

'''''''''''''''''''''''''''''''''''''''''''''''''

End Sub
 
Back
Top