VBA Shell Alternative to open Powershell when CMD is disabled in MS Access RRS feed

  • Question

  • I am using VBA in Access to open a command line application through PowerShell. I can't use Shell() because the command prompt is disabled, but not PowerShell.

    retval = Shell("powershell.exe", vbNormalFocus)

    This command returns Invalid Procedure Call or argument

    Set WshShell = CreateObject("WScript.Shell")
    Set oExec = WshShell.Exec("powershell.exe")

    This command returns Access is denied no matter what application I try to open.

    Application.FollowHyperlink "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"

    This command returns the message Unable to open c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe. No Program is registered to open this file.

    Dim wsh As WshShell
    Set wsh = New WshShell
    Debug.Print wsh.Run("powershell.exe", vbNormalFocus, True)

    This gives returns Permission denied.

    Tried CreateProcess, but does nothing.

    • Edited by Rick131 Friday, August 28, 2020 6:32 PM
    Friday, August 28, 2020 6:07 PM

All replies

  • I doubt this will work but try this.

    Option Explicit
      Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
            (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Sub OpenPowershell()
      Dim ret As Long
      On Error GoTo handler
      ret = ShellExecute(Application.hwnd, vbNullString, "Powershell", vbNullString, "C:\", 1)
      If ret < 3 Then
        Call MsgBox("Powershell is not found.", vbOKOnly, "Powershell Not Found")
      End If
    Exit Sub
      Call MsgBox(Err.Number & ": " & Err.Description, vbOKOnly, "Error Opening Powershell")
    End Sub

    Saturday, August 29, 2020 1:32 PM