Excel 2016 for Mac - set starting path for choose folder dialog

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
308
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I've written a Workbook in Windows, and am now trying to get it to work on Mac. One thing I can't figure out is how to tell Excel 2016/2019 for Mac to start at a particular folder when asking the user to choose a folder. Here's the code I'm using.

Code:
Sub GetFolderName_Mac()

'I modified code that I found on Ron de Bruin's website:
'https://www.rondebruin.nl/mac/mac017.htm

Dim RootFolder As String
Dim scriptstr As String

On Error Resume Next

If Val(Application.Version) < 15 Then 'Excel 2011 for Mac
        
    RootFolder = ThisWorkbook.Path
    
    scriptstr = "(choose folder with prompt ""Select the folder you want""" & _
        " default location alias """ & RootFolder & """) as string"

Else 'Excel 2016 for Mac or newer
    
    RootFolder = Replace(ThisWorkbook.Path, "/", ":")
    
    scriptstr = "return posix path of (choose folder with prompt ""Select the folder you want""" & _
        " default location alias """ & RootFolder & """) as string"

End If

MacFolderPath = MacScript(scriptstr)
On Error GoTo 0

'more code here to continue process

End Sub

If run on Excel 2011 for Mac, the code works. It does NOT work on Excel 2016 or 2019 for Mac. It seems to be because RootFolder is not quite correct. I had first tried setting RootFolder = ThisWorkbook.Path, but the MacScript command didn't like the slashes in the path. The variable has to have : as the path separator for that MacScript line to work. So that's why I am using the Replace command.

This is how RootFolder showing up on Excel 2016/2019 now:

:Users:mattc:Desktop:

I'm guessing I need the name of the hard drive at the beginning of the string, so it could look something like this:

macOS Sierra:Users:mattc:Desktop:

But 'ThisWorkbook.Path' doesn't return the hard drive name on Excel 2016/2019.

Anyone have any idea how I can get it to do what I want it to do? (I can't type in the hard drive name directly in the code, because my Workbook will be used by many different people.)
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I figured out a solution to my problem. After much trial & error, I figured out how to return the name of the hard drive on a Mac.

Code:
Dim HardDriveName As String
HardDriveName = MacScript("return (path to startup disk) as string")

The value of HardDriveName, in my case, was "El Capitan:"

I can now concatenate this to the other string, to get the full string that I need.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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