Running 'System Events' from Excel For Mac Via VBA Results in Invalid Procedure 5 Error - excel

I am trying to run a very simple apple script from VBA. The VBA code is:
NavigationJunk = AppleScriptTask("Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt", "NavigationMoveWindowLeftHandler", "")
The script reads:
on NavigationMoveWindowLeftHandler(Junk)
tell application "System Events" to key code 123 using {option down, control down}
end NavigationMoveWindowLeftHandler
where the complete path for the .scpt is: "/Users/Andrew/Library/Application Scripts/com.microsoft.Excel/Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt" (this is the correct path as documented here: Apple Script Task Throwing an Error 5
This code simply performs the key stroke cntrl+option+left arrow. That key stroke is a shortcut for the app Magnet to move the window to be half screen left.
This worked prior to updating my system to MacOS Ventura. I attempted to restart, uninstall Excel, reset the permissions in System Settings/Privacy/Automation (as documented here: Reset MacOS Privacy Settings) and reinstall Excel. When I tried to run the VBA code I was prompted to grant permission to system events and still received an invalid procedure error. I verified that when I call the handler from within the Apple Script Editor the system performs the keystrokes with no issues. I assume this is some sort of permission thing...How do I fix this?
Of note: this code called prior to the AppleScriptTask above runs just fine:
AppleScriptResult = Split(AppleScriptTask("Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt", "NavigationGetScreenResolution", ""), " ")
This is curious because the .scpt is the same file and calls a different handler:
on NavigationGetScreenResolution(Junk)
return do shell script "system_profiler SPDisplaysDataType | grep Resolution"
end NavigationGetScreenResolution
and runs just fine. Any help here would be greatly appreciated

Related

RTE5 using `Shell` in VBA

Background:
Recently, a macro I use via VBA (in Excel) to run a script has stopped working; this subroutine worked for the previous six years. This is on a work device in which I do not have full admin privileges. The script file is stored on a network drive which I have access to, and can open and double-click to execute.
I have read from multiple sources, each not having an answer (including here on SO), that people have begun to see RTE5 when using Shell(), which may be related to security settings from the administrator.
One suggestion was to use use ShellExecute, which I have not had luck using.
Issue:
I receive RTE5 (invalid procedure call or argument) on the line for Shell() within the below code.
Question:
Has anyone had this same issue and been able to resolve said issue? Please indicate how you resolved.
Code:
'To open file, which previously worked:
Dim Loc As String
Loc = "Z:/filename.bat"
Call Shell(Loc, 1)
'Attempt at using ShellExecute, which gives Network Access error
Dim Loc As String
Loc = "Z:/filename.bat"
Call CreateObject("Shell.Application").ShellExecute(Loc, 1)

Problem communicating with 3rd party apps via VBA on Mac M1

I have created a VBA add-in for Microsoft Word. Both a Windows and Mac version. The add-in communicates with 3rd party apps.
This has worked fine for years. I'm now struggling making this work for the Mac-version with M1-based macs (Maybe also Big Sur related).
The communication works partly via a dylib written i c, via stdin and stdout.
Secondly some functions use AppleScriptTask.
I can't make either of these two methods work.
AppleScriptTask always return the error:
Run-time error '5'
Invalid procedure call or argument
VBA-code:
s = AppleScriptTask("WMscript.scpt","Test","")
I tried scripts with .scpt, .scptd and .applescript.
Also newly created scriptfiles with simple test.script:
on Test(paramS)
tell application "Finder"
activate
end tell
end Test
The api works in other applications, but I can't open 3rd party apps in it when used from VBA.
I use these special folders for the all the 3rd party files to circumvent sandboxing for both methods:
/Library/Application support/Microsoft/Office365/User Content/Add-ins/
~/Library/Application Scripts/com.microsoft.word/
~/Library/containers/com.microsoft.word/Data/
I also tried the GrantAccessToMultipleFiles(FileArray) function. It always returns 'True' regardless which filenames are sendt in the array. It never prompt the user.
Running:
M1 Mac
Big sur 11.6 (M1)
Microsoft Word for Mac 16.54 (21101001)
Not sure following will help you.
If your third-party application is from unknown developer, then it goes to GateKeeper quarantine. Try to remove quarantine before calling your VBA script, running following script.
set theApp to application id "Avidemux.org" -- EDIT here the bundle ID
set quotedPath to quoted form of (POSIX path of (path to theApp))
do shell script "xattr -rd com.apple.quarantine " & ¬
quotedPath with administrator privileges
You can include the code above to beginning of your VBA script.
The ApplceScriptTask problem was related to an incorrect path, for the script file:
Incorrect: ~/Library/Application Scripts/com.microsoft.word/
Correct: ~/Library/Application Scripts/com.microsoft.Word/
Had to be Uppercase W.

How is AppleScriptTask called from Excel 365 VBA on Mac mini running Big Sur?

I am trying to run an AppleScript from VBA in an Excel 365 macro and I keep getting:
Run-time error '5': Invalid procedure call or argument
I have this script called "PythonCommand.scpt" in my /Library/Application Scripts/com.microsoft.Excel folder:
on PythonCommandHandler(pythonScript)
--do shell script "/usr/local/bin/Python3" & pythonScript
return "Handler ended! " & pythonScript
end PythonCommandHandler
I commented out the "do" statement so I should simply get back what I send it. I tested this in the script editor by adding a line to invoke the function and it works just fine.
I have this code in my VBA macro:
Dim result As String
Dim strPyScript As String
strPyScript = "xxxx"
result = AppleScriptTask("/Library/Application Scripts/com.microsoft.Excel/PythonCommand.scpt", "PythonCommandHandler", strPyScript)
and when I run it I get the error '5'.
I tried changing the first argument to just "PythonCommand.scpt" instead of the whole path but got the same error. I tried putting the last argument in as a quoted string instead of using a variable and got the same result. I have looked at this post:
How can I launch an external python process from Excel 365 VBA on OSX?
and started my coding from there (this example had the first argument with no path). Then I read this one:
https://learn.microsoft.com/en-us/office/vba/office-mac/applescripttask from Microsoft which is specific to using the AppleScriptTask command. It lays out the process a bit more clearly but is basically the same. I also looked at this post:
How to simply run an applescript task from mac excel 2016
which has a broken link to a Ron deBruin article which I found here:
https://macexcel.com/examples/setupinfo/applescripttask/index.html
Which is quite clear and easier to read but says basically the same thing. The post with the broken link was resolved by making the script an app and invoking it as a hyperlink. I tried that and it works but there are several shortcomings with that approach: can't pass an argument to the script, can't get anything back from the script, and control does not wait for the script to end before executing the next line of VBA code. I really want to make the AppleScriptTask command work. I feel I must be missing something. If others have gotten this to work I must be doing something wrong. I tried turning on all the references I could find in Tools References but that didn't change anything, I still got the error '5': message. Please help me out here if you can. I really appreciate any help you can offer.
Thanks
Phil
Phil,
The code I use is:
res = AppleScriptTask("selectFile.scpt", "GetFile", args)
NB I don't need to put the full path to the scpt file. I presume this is because it is sandboxed and so vba knows where it is.
BTW I put the scpt file in this folder:
${HOME}/Library/Containers/com.microsoft.Excel/Data/Library/Application Scripts/com.microsoft.Excel
The folder you have specified is not a user folder, but rather a system folder.

Why can my VB6 app not open the "Open" dialog on some systems

I have a VB6 application that, despite everything manages to work on lots of systems, including the numerous Windows 7 x64 systems.
On most of them, the windows dialogs accessed through COMDLG32.OCX work just fine. However, on one particular system, this doesn't work at all. Some forms, when attempting to show the "Open" (or "Save") dialog causes an exception:
Run-time error '32765'
The common dialog function failed during initalization. This error often occurs when insufficent memory is available
Although some other forms that use it simply never show the dialog box at all. Needless to say, memory is not an issue. Microsoft have a kb article on the error where they say:
You have a Microsoft Visual Basic 6.0 program that runs on a Terminal Server, the program uses the Common Dialog control to open a file, and the following conditions are true:
The user is using a roaming profile.
There is a policy to delete the roaming profile when the user logs off.
When these conditions are true, you may receive [the error quoted above]
The conditions mentioned are not true - this is all through the (only, local) user on the system. The suggested solution involves calling the relevant API calls directly.
Reluctant to implemenet the pile of code suggested in every form relevant, I found a class already built for the purpose - CDlgEx. While this works fine on all the computers where the OCX also works fine, on the system which has problems, whenver this line is reached:
RetValue = GetOpenFileName(OFN)
where
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
and OFN is of the private type expected by the function, nothing happens. No dialog box appears, and the program simply moves on to the next line.
Further testing has shown that this only happens when running from the IDE. Running from a compliled executable, everything is fine.
Had the exact same problem. Grab the cCommonDialog class from the following link and add it to your project.
https://github.com/ziggythehamster/ignitionserver/blob/master/vbtracer/cCommonDialog.cls
You can then do something like this in your code
Private Sub Command1_Click()
Dim commonDialog As New GCommonDialog
Dim fileName As String
commonDialog.VBGetOpenFileName fileName
MsgBox fileName
End Sub

Cannot export Sketch file to Zeplin. Forbidden 403 Error

I am new to both Sketch and Zeplin.
When I am exporting my artboard by pressing cmd + E everything seems to be fine.
Zeplin prompts me to select a project. When I select a project to export into, I get a notification saying "Exported 1 screen(s) to ProjectName".
However a short time later it gives me a error dialog and I cannot see any screen in my Zeplin project.
Here is the error dialog:
Note: My sketch app is in its trial period. But it's functioning well, besides this problem.
Thanks to #zeplin_io's fast support on Twitter I solved my problem.
Adjusting my Mac's Date & Time Pref.s to Auto solved issue.

Resources