none
New desktop cannot execute wsh.Run RRS feed

  • Question

  • Hello,

    Recently moved files to a new desktop & cannot execute a wsh.Run command on a batch file. The batch file calls 7zip. Both old & new PCs running Windows10, Office365 (Access 2016).

    Error message is: -2147024894 Automation error The system cannot find the file specified. I also tried the full path to the batch file. The full path will return errorCode = 1.

    Here is the code:

    Public Function ZipFile_WShell()
    On Error GoTo eh
        Dim wsh As Object
        Dim waitOnReturn As Boolean: waitOnReturn = True
        Dim windowStyle As Integer: windowStyle = 1
        Dim errorCode As Long
        Dim strBatchFile As String

    Set wsh = VBA.CreateObject("WScript.Shell")

    strBatchFile = Chr(34) & "7zip_batch.bat" & Chr(34)

    errorCode = wsh.Run(strBatchFile, windowStyle, waitOnReturn)

        If errorCode = 0 Then
            'continue
        Else
            MsgBox "Program exited with error code " & errorCode & "."
        End If

    End Function

    Thank you for any help!


    Albert S

    Saturday, January 18, 2020 10:11 PM

Answers

  • OK, I ran process monitor and found that the wsh.RUN was looking in a parent directory. Once I moved the batch file and the target file to that directory. It worked. Not sure why it won't accept the sub-directory as the file location. I'll post back if I can find the reason.

    Here is the process library output:

    C:\Users\***\Documents\7zip_batch.bat

    Should have been:

    C:\Users\***\Documents\Access_Dev\7zip_batch.bat

    Thank you,

    Albert


    Albert S

    • Marked as answer by Albert S Monday, January 20, 2020 6:13 PM
    Monday, January 20, 2020 6:13 PM

All replies

  • I would guess it's a security issue. For testing try with a batch file in the same folder as the Access app.

    You also would want to improve your error handling. Do not assume that CreateObject will succeed. wsh would then be Nothing, and wsh.Run would fail, possibly giving a false error.


    -Tom. Microsoft Access MVP

    Saturday, January 18, 2020 10:16 PM
  • Thank you for the reply.

    The batch file is in the same directory as the Access app. I can run the batch file, but not from vba.

    I see what you mean about CreateObject not giving an error if nothing happens. What would be a good way to test for that?

    Thank you,

    Albert


    Albert S

    Saturday, January 18, 2020 10:37 PM
  • OK, I ran process monitor and found that the wsh.RUN was looking in a parent directory. Once I moved the batch file and the target file to that directory. It worked. Not sure why it won't accept the sub-directory as the file location. I'll post back if I can find the reason.

    Here is the process library output:

    C:\Users\***\Documents\7zip_batch.bat

    Should have been:

    C:\Users\***\Documents\Access_Dev\7zip_batch.bat

    Thank you,

    Albert


    Albert S

    • Marked as answer by Albert S Monday, January 20, 2020 6:13 PM
    Monday, January 20, 2020 6:13 PM