Sending file to Recycle Bin

Senthil Murugan

New Member
Joined
Sep 25, 2024
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon to All

Could anybody tell me how to send a specific file in a folder to recycle bin ( not using Kill Function ) ?
 

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
Good Afternoon to All

Could anybody tell me how to send a specific file in a folder to recycle bin ( not using Kill Function ) ?
VBA Code:
Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
 
'Delete file Hello.xlsx
fso.DeleteFile "c:\Src\Hello.xlsx"
 
Upvote 0
Hi all,
be careful that the Kill statement deletes the file permanently, without moving it to the recycle bin, the same happens for the macro that uses the FileSystemObject. Below is the code for an alternative solution, adapt the path and file name:
VBA Code:
Private Declare PtrSafe Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Long

Private Type SHFILEOPSTRUCT
    hwnd As LongPtr
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As LongPtr
    lpszProgressTitle As String
End Type

Private Const FO_DELETE As Long = &H3
Private Const FOF_ALLOWUNDO As Integer = &H40
Private Const FOF_NOCONFIRMATION As Integer = &H10

Sub MoveToRecycleBin()
'https://www.mrexcel.com/board/threads/sending-file-to-recycle-bin.1265231/

    Dim fileOp As SHFILEOPSTRUCT
    Dim MyFile As String

    MyFile = "C:\Users\username\Downloads\Macros.xlsx" & vbNullChar '

    With fileOp
        .wFunc = FO_DELETE
        .pFrom = MyFile
        .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
    End With

    SHFileOperation fileOp
End Sub
 
Upvote 0
thanks for the feedback, glad to be of help, you can mark the thread as solved
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,642
Members
452,663
Latest member
MEMEH

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