Calling .sh from VBA

Nelsini

New Member
Joined
May 13, 2021
Messages
25
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
Hello!

We're currently moving from W10 to MacOS in my company and I'm trying to "translate" some VBA scripts we have. I'm having trouble with this one script that is used to delete files with +37 days (due to GDPR) on MacOS, and since I can't use FileSystemObject I created a .sh file that does that exact thing, but I need that to be assigned to a macro on the workbook.

I've tried different things, the file is on 757 permissions and I tried calling it a bunch of different ways and I get a "file not found" error when I run my macro (script works fine in terminal).

Code:
VBA Code:
Sub Mac_Delete()
Dim DelScript As String
Dim ScriptPath As String

ScriptPath = ActiveWorkbook.Path & Application.PathSeparator

DelScript = ScriptPath & "DeletionScript.sh"

RetVal = Shell(DelScript, vbNormalFocus)

I need the file path to be dynamic due to this being on a sharepoint folder and we sometimes move files around and are still organizing our folders.

Thank you in advance for any help given!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Forgive me - I know nothing about Mac or how its file system works, etc., so I could be completely off here. But if you were to pose the above problem but said it was in Windows, I would suggest that maybe you check to see if DelScript contains any spaces in the path. If it did, then the shell would interpret everything up until the first space as being full path of the file you wanted to execute and everything after it as an argument. The solution would be to wrap DelScript in double quotes. If this were on Windows...

Could that potentially be the issue?
 
Upvote 0
Hey, thank you for the response!

Yes, I suppose that could be the issue here, but how can I make the file path "dynamic" as I did above? It would probably need to be hard-coded no? I believe that would be a problem as this is an excel file used on a shared folder =/

I'll try to hard-code the file path when I can just to check if that's the problem, thanks!
 
Upvote 0
No no, you don't need to hardcode. Sorry, I wasn't being clear - when I say wrap it with double quotes, I mean something like:

VBA Code:
DelScript = chr(34) & ScriptPath & "DeletionScript.sh" & chr(34)

I'm guessing that Macs use the same character code numbers, but basically chr(34) is the code for the quotation mark "
And then that should work, if indeed the space in the path is the problem.
To save yourself time forever writing chr(34) all over the place, I usually use a simple function that wraps a string with quotation marks:

VBA Code:
Function DQ(ByVal FilePath As String) As String
    DQ = Chr(34) & FilePath & Chr(34)
End Function

So your code could be:

VBA Code:
Sub Mac_Delete()
Dim DelScript As String
Dim ScriptPath As String

ScriptPath = ActiveWorkbook.Path & Application.PathSeparator

DelScript = ScriptPath & "DeletionScript.sh"

DelScript = DQ(DelScript)

RetVal = Shell(DelScript, vbNormalFocus)
 
Upvote 0
Well, I tried your solution but for some reason, it doesn't seem to work but it doesn't output any error code, it simply just runs without performing the .sh instructions.
Maybe there's something wrong with the script itself?

Anyways, the team agreed on trying to move these files onto our VDI setups which are running W10 so this was a bit of wasted time :')

Thank you for your time, Dan!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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