Multiple Apps

Send a file to the Recycle bin

Ease of Use


Version tested with

97, 2000, 2002, 2003 

Submitted by:



A file will be sent to the recycle bin instead of being permanently deleted. Only files on the local machine can be sent to the Recycle Bin using this code, it will not work on a Citrix Server or any other Terminal Services software, not will it work on any network drive, mapped or not. 


Asking a user of your code to delete a file can be risky, this code can be used to reduce the risk inherent in users deleting the wrong file, giving you the option to restore it from the recycle bin. Tested with Excel 97, 2000, 2002(XP), 2003 and Word 2003 - should be fine with all vba enabled applications or VB. Requires NT4.0 or later or Windows 95 or later. 


instructions for use


Option Explicit 'To send a file to the recycle bin, we'll need to use the Win32 API 'We'll be using the SHFileOperation function which uses a 'struct' 'as an argument. That struct is defined here: Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As Long End Type 'now for the function declaration: Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long 'there are some constants to declare too Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 Private Const FOF_NOCONFIRMATION = &H10 Private Const FOF_SILENT = &H4 Function RecycleFile(FileName As String, Optional UserConfirm As Boolean = True, Optional HideErrors As Boolean = False) As Long 'This function takes one mandatory argument (the file to be recycled) and two 'optional arguments: UserConfirm is used to determine if the "Are you sure..." dialog 'should be displayed before deleting the file and HideErrors is used to determine 'if any errors should be shown to the user Dim ptFileOp As SHFILEOPSTRUCT 'We have declared FileOp as a SHFILEOPSTRUCT above, now to fill it: With ptFileOp .wFunc = FO_DELETE .pFrom = FileName .fFlags = FOF_ALLOWUNDO If Not UserConfirm Then .fFlags = .fFlags + FOF_NOCONFIRMATION If HideErrors Then .fFlags = .fFlags + FOF_SILENT End With 'Note that the entire struct wasn't populated, so it would be legitimate to change it's 'declaration above and remove the unused elements. The reason we don't do that is that the 'struct is used in many operations, some of which may utilise those elements 'Now invoke the function and return the long from the call as the result of this function RecycleFile = SHFileOperation(ptFileOp) End Function Sub foo() If RecycleFile("c:\logo_phpBB.GIF") <> 0 Then 'unable to delete file Stop End If 'Function returns zero on success, non zero on failure 'example errors: '1026 = File does not exist '1223 = Drive not found End Sub

How to use:

  1. Open the VBE by pressing Alt F11
  2. Insert a module by clicking Insert/Module from the menu
  3. Copy and paste all the code above

Test the code:

  1. Change the file name in Sub foo from c:\logo_phpBB.GIF to a sacrificial file on your computer
  2. Put your cursor into Sub foo and press F5

Sample File: 9.63KB 

Approved by mdmackillop

This entry has been viewed 111 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express