Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macros to move file to reycle bin?

Status
Not open for further replies.

dbevins

Technical User
Nov 29, 2001
12
US
I can't find the location of the recycle bin folder in Windows XP Pro -- I'm trying to write a Word 2002 macro to move a file to the recycle bin. I don't want to "kill" or "filedelete" because I want to able to recover it if need be. Thanks -- Doug
 
Code:
'This program needs a Common Dialog Box, named CDBox.
'  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
'   and select Microsoft Common Dialog control)
Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: [URL unfurl="true"]http://www.allapi.net/[/URL]
    'E-Mail: KPDTeam@Allapi.net
    Dim SHFileOp As SHFILEOPSTRUCT
    'Set the dialog's title
    CDBox.DialogTitle = "Select a file to delete ..."
    'Set the dialog's filter
    CDBox.Filter = "All Files (*.*)|*.*"
    'Show the 'Open File' dialog
    CDBox.ShowOpen
    With SHFileOp
        'Delete the file
        .wFunc = FO_DELETE
        'Select the file
        .pFrom = CDBox.filename
        'Allow 'move to recycle bn'
        .fFlags = FOF_ALLOWUNDO
    End With
    'perform file operation
    SHFileOperation SHFileOp
    MsgBox "The file '" + CDBox.filename + "' has been moved to your Recycling Bin !", vbInformation + vbOKOnly, App.Title
End Sub
 
JustinEzequiel's [Hey, where have you been??] code example, taken from AllApi.net, is specifically intended for standalone Visual Basic, hence the reference to including the MS Common Controls dialog through the Tools menu. Some modification is needed for Word VBA. I have created a function wrapper that calls the relevant Shell32 function. The following code should be placed into a standard code module:
Code:
Type SHFILEOPSTRUCT
  hWnd As Long
  wFunc As Long
  pFrom As String
  pTo As String
  fFlags As Integer
  fAborted As Boolean
  hNameMaps As Long
  sProgress As String
End Type

Const FO_DELETE = &H3
Const FOF_ALLOWUNDO = &H40
Const FOF_NOCONFIRMATION = &H10

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


Sub TestFileDeletion()
Dim FSpec As String
Dim Result As Boolean

   FSpec = "C:\Temp\Test.txt"
   
   Result = DeleteFileToRecycleBin(FSpec)
   MsgBox "File " & FSpec & " deletion to Recycle Bin - Success = " & Result
   
End Sub



Function DeleteFileToRecycleBin(ByVal FName As String, Optional ByVal NoConfirmation As Boolean = False) As Boolean

Dim SHFileOp As SHFILEOPSTRUCT
Dim FlagValues As Long


   FlagValues = FOF_ALLOWUNDO
   If NoConfirmation Then
     FlagValues = FlagValues + FOF_NOCONFIRMATION
   End If
   
   With SHFileOp
     'Delete the file
     .wFunc = FO_DELETE
     'Select the file
     .pFrom = FName
     'Allow move to Recycle Bin
     .fFlags = FlagValues
   End With
    
   'perform file operation
   DeleteFileToRecycleBin = (SHFileOperation(SHFileOp) = 0)

End Function
The function takes as parameters the fully qualified pathname of the file to be deleted and an optional boolean value indicating whether the user should be prompted to confirm the deletion. Procedure TestFileDeletion is, of course, for demonstration. The path & filename are what I used to test the code. Keep in mind that the return value of DeleteFileToRecycleBin only indicates whether an error occurred, not whether the file was actually deleted. In other words, the function will return True even if the user cancels the deletion when prompted (assumes NoConfirmation = False).

Hope this helps.

Regards,
Mike
 
> [Hey, where have you been??]
been busy working; we've been moving to open source
technologies so I haven't been keeping in touch much
with MS products

> code example, taken from AllApi.net, is specifically
> intended for standalone Visual Basic
thought he could figure it out on his own :)
(or I was too lazy to check)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top