|
|
|
|
|
|
Multiple Apps
|
Send a file to the Recycle bin
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
97, 2000, 2002, 2003
|
Submitted by:
|
zilpher
|
Description:
|
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.
|
Discussion:
|
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.
|
Code:
|
instructions for use
|
Option Explicit
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
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
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
Dim ptFileOp As SHFILEOPSTRUCT
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
RecycleFile = SHFileOperation(ptFileOp)
End Function
Sub foo()
If RecycleFile("c:\logo_phpBB.GIF") <> 0 Then
Stop
End If
End Sub
|
How to use:
|
- Open the VBE by pressing Alt F11
- Insert a module by clicking Insert/Module from the menu
- Copy and paste all the code above
|
Test the code:
|
- Change the file name in Sub foo from c:\logo_phpBB.GIF to a sacrificial file on your computer
- Put your cursor into Sub foo and press F5
|
Sample File:
|
RecycleBin.zip 9.63KB
|
Approved by mdmackillop
|
This entry has been viewed 113 times.
|
|