excelvbashell

Can't get WSCript.Shell object's Run method to work


I set up the following test procedure.

Private Sub TryPDF()
    
    Dim oShell          As Object
    Dim App             As String
    Dim SrcPath         As String
    Dim Fn              As String

    App = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe "
    SrcPath = Environ("UserProfile") & "\Downloads\"
    Fn = "20200509_Order_of_08_05_2020.PDF"

    Shell App & SrcPath & Fn, vbNormalFocus             ' opens the file
    
    Set oShell = CreateObject("WSCript.Shell")
    oShell.Run App & SrcPath & Fn, vbNormalFocus, True  ' error -2147024894
    Set oShell = Nothing
End Sub

The Shell command in the middle works, thereby proving that app and files exist as and where specified. However, I want the Wait property of the WSCript.Shell object and therefore want to open the file using the line oShell.Run App & SrcPath & Fn, vbNormalFocus, True. I have tested it as shown and without some and any parameters, which should just open the Acrobat Reader when totally stripped, and I always get the same error, "Method 'Run' of object 'IWshShell3' failed".

What am I doing wrong?


Solution

  • You have to escape the spaces in your paths (app and file) by surrounding them with double-Quotes, as the command-line uses them as argument separators. That would make

    C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe
    

    C.\Program a file named Program located on C: and that doesn't exists!

    "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
    

    will work as expected on command-line.

    For some reasons VBA.Shell can handle the spaces in the apppath, but WScript.Shell can't. Both will fail on not quoted paths to file, if they contain spaces.

    My prefered quoting-style is the Chr(34) function

    CommanQuoted = Chr(34) & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & Chr(34) & " " & Chr(34) 
     & "...\Downloads\20200509_Order_of_08_05_2020.PDF" & Chr(34)
    

    as it is far better readable than the also useable double double-quote

    CommandQuoted = """C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"" ""...\Downloads\20200509_Order_of_08_05_2020.PDF""" 
    

    Or you can create a constant that returns a double quote

    Const dquote As String = """"
    CommandQuoted = dquote & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & dquote & " " & dquote 
     & "...\Downloads\20200509_Order_of_08_05_2020.PDF" & dquote
    
    Private Sub OpenPDFWithWScripShell()
        Dim oShell          As Object
        Dim AppPathQuoted   As String
        Dim SrcPathQuoted   As String
        Dim FileName        As String
        Dim shellCommand As String
        AppPathQuoted = Chr(34) & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & Chr(34)
        FileName = "20200509_Order_of_08_05_2020.PDF"
        SrcPathQuoted = Chr(34) & Environ("UserProfile") & "\Downloads\" & FileName & Chr(34)
        shellCommand = AppPathQuoted & " " & SrcPathQuoted
        Debug.Print shellCommand
        
        Set oShell = CreateObject("WSCript.Shell")
        oShell.Run shellCommand, vbNormalFocus, True
        Set oShell = Nothing
    End Sub