Select files on a Mac (GetOpenFilename)

In Windows we can use for example GetOpenFilename to select files and do what we want with the path results, you can use filefilter to only display the files you want and use MultiSelect to select more than one file. Also it is possible with ChDrive and ChDir to set the folder that is selected when GetOpenFilename opens.

But on a Mac the filefilter is not working and it is not possible to select more than one file. Also ChDir is not working like in Windows to set the folder that will open with GetOpenFilename. But we can use a combination of VBA and Applescript, see the example below that only let you select xls, xlsx and xlsm files, and you can also set the start folder.

Important : The file location can be very important in Mac Excel, read the information on this webpage : Problems with Apple’s sandbox requirements in Mac Office

Example for Mac Excel

You can run the macro below without changing it, it opens the Desktop in Finder and you can only select one xls, xlsx or xlsm file, see the code how to change this for selecting more than one file or other file formats. For testing it will open each file you select and show a msgbox and close it.

Other File formats

In the macro you see this code line that say which file format you can select, xls, xlsx and xlsm). You see that I add 3 file formats, but you can change that if you want to also add for example xlsb files.

FileFormat = "{""com.microsoft.Excel.xls"", ""org.openxmlformats.spreadsheetml.sheet"",""org.openxmlformats.spreadsheetml.sheet.macroenabled""}"

If you only want one format you can use this to be able to only select xlsx files for example.

FileFormat = "{""org.openxmlformats.spreadsheetml.sheet""}"

This is a list of a few formats that you can use :
  • xls : com.microsoft.Excel.xls
  • xlsx : org.openxmlformats.spreadsheetml.sheet
  • xlsm : org.openxmlformats.spreadsheetml.sheet.macroenabled
  • xlsb : com.microsoft.Excel.sheet.binary.macroenabled
  • csv : public.comma-separated-values-text
  • doc : com.microsoft.word.doc
  • docx : org.openxmlformats.wordprocessingml.document
  • docm : org.openxmlformats.wordprocessingml.document.macroenabled
  • ppt : com.microsoft.powerpoint.ppt
  • pptx : org.openxmlformats.presentationml.presentation
  • pptm : org.openxmlformats.presentationml.presentation.macroenabled
  • txt : public.plain-text
  • pdf : com.adobe.pdf
  • jpg : public.jpeg
  • png : public.png
  • QIF : com.apple.traditional-mac-plain-text
  • htm : public.html
05/09/2024
Web design by Will Woodgate