Reputation: 41
I have used the Shell command in MS Access for several years in multiple databases in various versions. Most recently, I have received this Run-Time error 5 - invalid procedure call or argument on a Shell command I have used for many years. Can anyone tell me if there was a recent Windows update or possibly MS Office update that has caused this error to appear? I am currently using Office 2010 and Office 2013 and receiving the same error in both version on multiple computers. I have even tried creating a simple empty database and gone straight to the vba window and in the immediate window tried a simple Shell command as such:
Shell "C:\Notepad.bat", vbNormalFocus
Notepad.bat is a simple command inside it:
cmd /C "Notepad.exe"
The code:
Shell "Notepad.exe", vbNormalFocus
does work, but:
Shell "Cmd.exe", vbNormalFocus
still gives the same Error 5 message on every machine I test it on. Running the batch files from the Windows explorer works just fine. Any help is much appreciated.
Upvotes: 1
Views: 8294
Reputation: 347
First accept that you are working with a Microsoft Office product. Take a deep breath and some tranquilizers.
Accept, that the error message you get may have nothing to do with the problem. Accept, that Windows may have paranoid settings, which prevent you to perform your work.
Having all that said:
removing it, as follows:
cmd = "put your command here"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("tmp_file.txt")
oFile.WriteLine cmd
oFile.Close
Set fso = Nothing
Set oFile = Nothing
Name "tmp_file.txt" As "tmp_file.bat"
Shell "tmp_file.bat", vbNormalFocus
Kill "tmp_file.bat"
Upvotes: 0
Reputation: 853
For anyone stumbling upon this issue, Windows Defender can also be the cause of this behavior.
You need to check if your Security Admin configured the Attack Surface Reduction capability of Windows Defender (through Intune, MEM, GPO...).
The specific rule you need to look for here is Block all Office applications from creating child processes (d4f940ab-401b-4efc-aadc-ad5f3c50688a
).
You can easily verify this in the Windows Event Viewer, in Application Logs, Microsoft, Windows, Windows Defender and then Operational and looking for warning events of ID 1121.
Here is a screen capture (sorry, it's in French):
Note that only the GUID of the rule is shown, not the full name (the list of GUID can be found in the reference).
Or if you prefer using Powershell:
Get-WinEvent -ProviderName "Microsoft-Windows-Windows Defender" | Where-Object { $_.Id -eq '1121' }
Upvotes: 6
Reputation: 41
After a lot more investigation, I happened on a new software installed by our network software engineers that maintain our McAfee protection. Last week, there was a blind install of a new app called Adaptive Threat Protection and is set to check for MS office apps running shell commands, especially the CMD type. This is set to automatically block all of these such commands. I finally came to a conclusion to look in anything recently installed or updated on my computer and happen to find a few different things installed within the last week date range. Thank you for your assistance and answers so far. The answer provided by thx1138v2 were something I also tried prior to my post but I figured for my 1st question I tried to keep my post to a bit of a minimum.
Upvotes: 3