Bin File using VBA

  • Thread starter Thread starter dl
  • Start date Start date
D

dl

To delete a file from the file sytem you use the VBA command

Kill

Is there a way to rather delete and send the file to the bin/trash can on
the computer using VBA?
 
Chip Pearson posted this:

You have to do it via a Windows API call -- there is no built-in VBA way to
do it.

'----------------------------------------------
Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40

Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long


Sub RecycleFile(FileName As String)
Dim lReturn As Long
Dim sFileName As String
Dim FileOperation As SHFILEOPSTRUCT

With FileOperation
.wFunc = FO_DELETE
.pFrom = FileName
.fFlags = FOF_ALLOWUNDO
End With

lReturn = SHFileOperation(FileOperation)

End Sub
'----------------------------------------------

Then call this with

RecycleFile FileName:="G:\Temp\Book1.xls"
 
Back
Top