1

In VBA I am running a piece of external software I wrote. I know the software works well. I can run it from the command line. But when I attempt to execute it using WScript.Shell.Run, it returns a 1 and it never runs. I can't even tell that it executes the software at all.

Here is the executing portion of the VBA class.

Public Function Execute() As Integer
    Dim wsh As Object
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    Dim exitCode As Integer: exitCode = -1

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

    'Run program with arguments and wait for the program to finish 
    If Me.PrinterType = 1 Then exitCode = wsh.Run(Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location & "", windowStyle, waitOnReturn)
    If Me.PrinterType = 2 Then exitCode = wsh.Run(Me.Path & " /boxid=" & Me.Location & " /version=" & Me.Version, windowStyle, waitOnReturn)
    Execute = exitCode

End Function

An example of the full execute string:

C:.path.to.\PrintLabel.exe /serial=EOSJ61110044 /position=2

Evidence that the script runs without error from the same machine the VBA is executing on: enter image description here

So I've got no idea what's going on.

I can use the following VBA Shell function successfully:

If Me.PrinterType = 1 Then exitCode = Shell(Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location, vbHide)

But the problem then becomes that the VBA doesn't wait on the software to finish before continuing. So I need to continue to try and make WScript.Shell work. What am I missing? Are there any specific references that need to be enabled that I could be missing?

Update 1:

Tried this to mimic this SO post, but still didn't have success:

If Me.PrinterType = 1 Then fullExecutionString = Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location
If Me.PrinterType = 2 Then fullExecutionString = Me.Path & " /boxid=" & Me.Location & " /version=" & Me.Version
wsh.Run fullExecutionString, windowStyle, waitOnReturn

Update 2: Made a small method that repeats the process in windows cmd console. It works (more or less) with the same code. I am guessing it has something to do with the command path string with arguments?

Private Sub Button2_Released()
    Dim wsh As Object
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    Dim exitCode As Integer: exitCode = 0
    Set wsh = VBA.CreateObject("WScript.Shell")
    exitCode = wsh.Run("C:\Windows\System32\cmd.exe", windowStyle, waitOnReturn)
End Sub
addohm
  • 2,248
  • 3
  • 14
  • 40

1 Answers1

0

Answering my own question:

After much debugging I resolved that the path with spaces is causing the issue. If I directly input the following (notice the quote placement), the code successfully runs:

exitCode = wsh.Run("""C:\path.to\PrintLabel.exe"" /serial=EOSJ61110044 /position=2", windowStyle, waitOnReturn)

So in the end I adjusted my Path property:

Public Property Let Path(ByVal NewValue As String)
   actPath = Chr(34) & NewValue & Chr(34)
End Property
Public Property Get Path() As String
    Path = actPath
End Property
addohm
  • 2,248
  • 3
  • 14
  • 40