Enable Microsoft Scripting Runtime Excel For Mac

Автор:

There are a number of problems associated with creating a spreadsheet that works on Apple macs and Windows which can select and open files using a 'open file' dialog box

VBA (Excel) Dictionary on Mac? Use a Mac plugin that enables use of Dictionaries on Macs (my favorite option if one exists) Do some kind of variable switch like the following: isMac = CheckIfMac If isMac Then ' Change dictionary variable to some other data type that is Mac friendly and provides the same functionality.

  • Difficulty in creating a fileopen dialog box for macs
  • Error 1004 returned when refreshing a queryTable after the filename has been changed
  • Writing vba that works on both OSs given the range of functions that are required on one, but generate fatal errors on the other.

The following code provides solutions to most of these problems, opening three text files using tableQueries based on the name of one of the files that is found using a find file dialog box.

Browser specific fileopen dialog box

The following is the Windows version of the code for opening a dialog box for selecting a file

And this is for macs:

The fileopen dialog box on a mac is best implemented with applescript. This script catches errors within the Apple script and returns the error number as text (which always begins with a '-') allowing the calling code to decide what to do. This catches 'errors' such as clicking the cancel button and opening with a directory that does not exist. I was not able to identify detailed error info if I caught the error in the VBA.

Enabling file access on Excel 2016 for macs

In the 2016+ versions of Excel, VBA operates in a sandbox which means that explicit permission has to be granted if any files are to be opened. This is not required if the file has been identified with a appleScript 'fileopen dialog box. For other files this is granted using the GrantAccessToMultipleFiles method. This is wrapped in a method because if it is encountered in code prior to Excel 2016, or on a mac then it throws an untrappable error

Getting the directory from a full path

Can't unmount disk mac erase. This gets the directory from a full path

Browsing for a filename

This the function called by the 'Browse' button, which calls the OS specific browse subroutine and handles the responses for the mac filedialog, and then goes into common code to see whether the returned value should be placed in the spreadsheet and the query tables loaded using Load_Click

Loading queryTables

This method is either called as part of the processing when 'Browse' is clicked, or can be called independently to refresh the files if the names have not changed. It makes use of three queryTables that have already been placed in the appropriate positions in the spreadsheets. The 'Browse' button selects the filename which ends _results.txt which is associated with the first queryTable. The other two filenames are derived from the _results.txt filenames Because they have not been explicietly selected by the user the mac security requires that the user confirms that it is OK to load them.

On a mac the code has to deal with the fact that the mac fileOpen dialog box uses path variables with a ':' separator bu the grant access function uses a '/' separator and must start with '/User..' Ibm odbc driver.