Excel "Scripting.FileSystemObject" not releasing folder - *Permission Denied* when trying to rename it

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
My apologies for the verbose title. I have a group of 4 folders whose names I alter through VBA. However, if I open one of those folders programmatically, it will subsequently not allow me to rename that particular folder either programmatically or manually (in file explorer) until I shut down my excel. When I restart my excel, it works perfectly as long as I do not open the folder through VBA. So it's like Excel is taking hold of that folder when I open it through excel and doesn't return it (error: Permission Denied when I try to remain it through VBA). Has anyone come across this?

Here's my code:

VBA Code:
Dim oFSO As Object, mySource As Object, folder As Variant
Dim lNewItemNum As Long
Dim sMyPath As String, sPrefix As Stringt
Dim lNewNum As Long
Dim sNewName As String


Set mwSht = ThisWorkbook.Worksheets("Settings")
Set mrRange = mwSht.Range("FolderLocation")
sMyPath = mrRange.Value & "\" & Me.cbType & "\" & Me.tbCharterID

Set oFSO = CreateObject("Scripting.FileSystemObject")

If DirectoryExists(sMyPath) = False Then
    oFSO.CreateFolder sMyPath
End If

Set mySource = oFSO.GetFolder(sMyPath)

'MOVE / REARRANGE
If sMode = "Rearrange" Then
    For Each folder In mySource.subFolders
        lNewItemNum = moDelivFinalDict(folder.Name)
        sPrefix = Left(folder.Name, 18)
        sNewName = sPrefix & Format(lNewItemNum, "000") & "A"
        folder.Name = sNewName  '<-----------------------------------------ERROR OCCURS HERE "PERMISSION DENIED"
        'Note that the line above runs perfectly if I do not programmatically open the folder in another method 
    Next folder
    'Now we should have all the folders correctly named but they all have A that need removing
    For Each folder In mySource.subFolders
        folder.Name = Left(folder.Name, 21)
    Next folder
    Call ReAdjustNumbering
End If

Set oFSO = Nothing
Set mySource = Nothing
 Set folder = Nothing

End Sub

Here is the code that stores or views files. If I don't run this code the above code works perfectly. If I "Open" any files, that's when the above code fails.

VBA Code:
Public Sub iDocDropOrOpen(sMode As String)

'We want to copy a file to our folder to msFilePath
Dim myFile As Variant
Dim sFileCopied As String
Dim oFSO As Object, Shex As Object
Dim vParts As Variant
Dim i As Long
Dim file As Variant, vFileSelected As Variant


Set oFSO = CreateObject("Scripting.FileSystemObject")

Set myFile = Application.FileDialog(msoFileDialogOpen)
If sMode = "Drop" Then
    With myFile
        .Title = "Store file for this deliverable!"
        .AllowMultiSelect = True
        .InitialFileName = Environ("USERPROFILE") & "\"
        If .Show <> -1 Then
            Exit Sub
        End If
        For Each file In .SelectedItems
            vFileSelected = file
            vParts = Split(vFileSelected, "\")
            sFileCopied = msFilePath & "\" & vParts(UBound(vParts))
            Call oFSO.CopyFile(vFileSelected, sFileCopied, True)
        Next file
    End With
    MsgBox "Your file has been successfully stored!", vbInformation
ElseIf sMode = "Open" Then
    With myFile
        .Title = "Choose File"
        .AllowMultiSelect = True
        .InitialFileName = msFilePath & "\"
        If .Show <> -1 Then
            Exit Sub
        End If
        For Each file In .SelectedItems
            vFileSelected = file
            Set Shex = CreateObject("Shell.Application")
            Shex.Open vFileSelected
        Next file
    End With
End If

'Clean up
Set oFSO = Nothing
Set myFile = Nothing
Set Shex = Nothing
Set file = Nothing
Set vFileSelected = Nothing

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
So doing some more research on this. The excel application locks the folder after storing a file. You can see at the second snippit of the code above that I try to set all the objects to "nothing" in order to try to unlock the folder. Even if I open 'file explorer' and try to rename the folder I get 'permission denied'. It takes the closing of the excel file to release the folder (i.e. I can rename the folder after closing the excel file). Has anyone come across this and solved it?
 
Upvote 0
Same thing happens manually. If you open an excel workbook manually, you shouldn't be able to rename the folder it is in while it's open.
 
Upvote 0
Hey Rory,

The excel file is not in the folder in question. The folders store files about a particular job. It's interesting that I have no problem until I use the code to Open one of the files (say a jpeg file). I even moved the excel file to the desktop to no avail.
 
Upvote 0
I didn't mean that the location of the excel file with the code was key. I just meant that you shouldn't be able to rename a folder when its files are open.
 
Upvote 0
I agree with that... but I have closed the file before running the renaming code. So I use the excel file to open the file, close the file, and then run the code to rename folders. All folders rename except for the one that held the open file.

So I have a button that opens a folder and it has 2 PDFs say, and maybe a jpeg. I use the SHEX Object to open it and that works. I close the PDF (or jpeg) and run into the issue with the folder that held those files.
 
Upvote 0
I wonder if the "SHEX" shell command is the issue here. Is there a way to open up files otherwise to test this theory?
 
Upvote 0
Looks like someone on this form had something similar to my isssue:

 
Upvote 0
Yeah, that part was kind of important... oops! ;)

Trying to find examples of "Shell Execute" with little luck. How would my code change?

VBA Code:
For Each file In .SelectedItems
       vFileSelected = file
       Set Shex = CreateObject("Shell.Application")
       Shex.Open vFileSelected
Next file

I did find this but it seems a bit over the top?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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