is a function in VBA (Visual Basic for Applications) that allows you to run AppleScript code from within a VBA macro in Microsoft Excel for Mac. By using this function, you can perform tasks on your Mac that would otherwise be outside the capabilities of VBA, essentially extending your VBA script’s functionality to control other applications on macOS.
Since Microsoft has been updating Excel for Mac, the MacScript
function was deprecated in newer versions. If you’re using Excel 2016 or later for Mac, you won’t be able to use MacScript
directly as it used to be in older versions. Instead, you might need to use AppleScriptTask to call an AppleScript script stored on the disk.
However, if you are indeed working with an older version of Excel for Mac that supports the MacScript
function, you can use it like this:
1. Open Microsoft Excel for Mac.
2. Press Alt
to open the VBA Editor.
3. Insert a new module or use an existing one in the VBA project.
4. Write a VBA macro that includes the MacScript
function, using AppleScript syntax for the script part.
Here’s an example VBA macro using MacScript
(in older versions of Excel for Mac):
Dim script As String
Dim result As String
' AppleScript command to say "Hello World"
script = "say ""Hello, World"""
' Run the AppleScript command via MacScript
result = MacScript(script)
' Optionally, use the result
In the above example, the MacScript
function is used to execute a simple AppleScript command that instructs the Mac to audibly say “Hello, World”.
If you are working with Excel 2016 or later for Mac and need to execute AppleScript, you need to save your AppleScript on disk and call it using AppleScriptTask
as outlined below:
1. Write an AppleScript script and save it with the scpt
extension in the ~/Library/Application Scripts/com.microsoft.Excel
folder. For example, create a file named HelloWorld.scpt
2. Your AppleScript could look like this:
on run argv
say “Hello, World”
3. From your Excel VBA module, use the AppleScriptTask
function to call your AppleScript file.
Dim result As String
result = AppleScriptTask("HelloWorld.scpt", "run", "")
Remember that AppleScriptTask
is the approach for automation scripts in newer Excel for Mac versions where direct inline AppleScript execution via MacScript
is no longer supported.
Unlock Your Potential
Help us grow the project