vbamacosms-wordms-office

Can't run popen command in office vba script, Run-time error '32815'


I'm running Word 16.63 (22070801) on Mac OS BigSur.

And I'm trying to invoke popen in VBA scripts (on MacOS):

Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long

Sub AutoOpen()
    a = popen("whoami", "r")
End Sub

But it results in runtime error:

enter image description here

Everything looks fine to me, does anyone know what's wrong?


Solution

  • The runtime error code 32815 happens when system policy is preventing Microsoft Office apps from loading the external .dylib files.

    The policy is called Visual Basic external library bindings and its key is DisableVisualBasicExternalDylibs. This policy cannot be enabled/disabled in the application's user interface.

    The policy is set globally for all Office apps on macOS. It cannot be set on a single app only.

    To enable the policy, run this command:

    defaults write com.microsoft.office DisableVisualBasicExternalDylibs -bool true
    

    Any VBA code which calls external C function loaded from a .dylib will throw the 32815 error now.

    To disable the policy, delete it:

    defaults delete com.microsoft.office DisableVisualBasicExternalDylibs
    

    Documentation

    Read more about preferences for macro security in Office for Mac.

    The policy can be also set by macOS MDM profiles, or using JAMF configuration.