Macro that selects and deletes a file in Excel for Mac

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
I use Microsoft® Excel for Mac, Version 16.83 (24031120), Office LTSC Standard for Mac 2021.
I have designed a code to open a dialog box and allow me to select any file; and, once selected, to give me the option to move it to the trash. Everything seems to work correctly showing, via a screen, that; "The file has been moved to the trash successfully." But it hasn't actually moved it to the bin, it's still in the same place.
How can I fix this problem and have it actually move /Delete the file I have selected?
This is the code:
VBA Code:
Sub SelectAndDeleteFile()
    Dim RutaArchivo As String
    Dim Respuesta As Integer
    ' Select file
    RutaArchivo = MacScript("return POSIX path of (choose file)")
    
    If RutaArchivo <> "" Then
        Respuesta = MsgBox("¿Surely you want to move the file to the trash?", vbYesNo + vbQuestion, "Confirmation")
        If Respuesta = vbYes Then
            On Error Resume Next
            Kill RutaArchivo ' Delete the file (move to trash)
            If Err.Number = 0 Then
                MsgBox "The file has been successfully moved to the Trash..", vbInformation, "File Deleted"
            Else
                MsgBox "Failed to move file to trash.", vbExclamation, "Error"
            End If
            On Error GoTo 0
        End If
    End If
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I have never coded for Mac so maybe this won't help. First, I'd comment out the On Error Resume Next and re-test to be sure that isn't the problem. Then there is this:
 
Upvote 1
Try implementing the following changes:
VBA Code:
...
RutaArchivo = MacScript("return POSIX path of (choose file) as String")
...
' Instead of Kill, use
MacScript "tell application ""Finder"" to delete """ & RutaArchivo & """"
...
 
Upvote 1
Try implementing the following changes:
VBA Code:
...
RutaArchivo = MacScript("return POSIX path of (choose file) as String")
...
' Instead of Kill, use
MacScript "tell application ""Finder"" to delete """ & RutaArchivo & """"
...
The modifications you propose have been carried out, it still does not work. It does not delete the file. I attach a screenshot
 

Attachments

  • Captura de Pantalla 2024-04-02 a las 19.57.35.png
    Captura de Pantalla 2024-04-02 a las 19.57.35.png
    15.6 KB · Views: 1
Upvote 0
Unfortunately, I cannot be of further help to you as I have an older version of Excel for Mac (14.7.7) where the suggested changes were tested to work. Hope someone with a newer version will chime in.
 
Upvote 1
Thank you very much, Tetra201. In the end, I have come up with the solution, which is to use a script, within the code, that allows me to delete the file, and with the rest of the code to open a dialog box and allow me to select any file. I leave you the complete code in case you need it.
I also want to thank Micron, for his valuable contribution that has allowed me to complete this code.
VBA Code:
Sub DeleteFile()
    Dim RutaArchivo As String
    Dim scriptToRun As String
    Dim Respuesta As Integer
    
    ' Select file
    RutaArchivo = MacScript("return POSIX path of (choose file) as String")
    
    If RutaArchivo <> "" Then
        Respuesta = MsgBox("¿Are you sure you want to delete the file?", vbYesNo + vbQuestion, "Confirmation")
        If Respuesta = vbYes Then
            ' Build AppleScript script to delete the selected file
            scriptToRun = "do shell script ""rm "" & quoted form of POSIX path of """ & RutaArchivo & """"
            
            ' Execute AppleScript script
            On Error Resume Next
            MacScript scriptToRun
            If Err.Number = 0 Then
                MsgBox "The file has been successfully deleted.", vbInformation, "File Deleted"
            Else
                MsgBox "Error deleting file.", vbExclamation, "Error"
            End If
            On Error GoTo 0
        End If
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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