Delete File or Folder VBA Issue

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
586
Office Version
  1. 2013
Platform
  1. Windows
The code below works to delete a File or Folder from within a excel Spreadsheet Using the selected cell that contains the full path of either the file or folder.

The issue I'm having is that I want to have some sort of safety that it just does not delete the file but it also appears in their recycle bin.

But for some reason I just cannot get any code to delete the file and it appears in the recycle bin any assistance will be appreciated.

VBA Code:
Sub DeleteFileOrFolderBasedOnCellPath()
    Dim fso As Object
    Dim Path As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    Path = ActiveCell.Value  ' Get the path from the selected cell
    If fso.FileExists(Path) Then  ' Check if the path is a file
        On Error Resume Next
        Kill Path  ' Delete the file
        On Error GoTo 0
        If Not fso.FileExists(Path) Then  ' Check if the file is deleted
            MsgBox "File deleted successfully."  ' Display success message
        Else
            MsgBox "Failed to delete the file."  ' Display failure message
        End If
    ElseIf fso.FolderExists(Path) Then  ' Check if the path is a folder
        On Error Resume Next
        fso.DeleteFolder Path, True  ' Delete the folder
        On Error GoTo 0
        If Not fso.FolderExists(Path) Then  ' Check if the folder is deleted
            MsgBox "Folder deleted successfully."  ' Display success message
        Else
            MsgBox "Failed to delete the folder."  ' Display failure message
        End If
    Else
        MsgBox "File or folder not found."  ' Display message if the file or folder doesn't exist
    End If
    Set fso = Nothing
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
AFAIK, files deleted via vba are not recoverable. If you think there is a chance that after files have been deleted via code that they will need to be recovered, then you should archive them instead. One means of doing that would be to move them to an "archive" folder
 
Upvote 0
I just did a quick Google search, and found this...


Hopefully it helps!
 
Upvote 0
I must confess that I'm not 100% up to speed with API calls on 32 vs 64 bit Office but that looks like it was written for 64 bit, and I suspect that OP's 2013 Office version is 32 bit, so it would need conditional compilation if he/she decides to try it.
 
Upvote 0
Actually, the API will work on Office 2010 and later versions, whether it's a 32-bit or 64-bit version. Also, if all users will be using Office 2010 or later, there's no need for conditional compilation.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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