VBA WScript stopped working

The following code worked fine yesterday, but not today.
The file "Swiss_rob.bat" does get updated.
If I go to "File Explorer", and double-click "Swiss_rob.bat", it runs fine.


Sub Swiss()

    Dim OutputPath As String
    Dim objShell

    OutputPath = ThisWorkbook.Path
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile(OutputPath & "\Swiss_rob.bat", True)
    a.writeline Range("Command")
    a.Close
    Set objShell = CreateObject("WScript.Shell")
    cmd = OutputPath & "\Swiss_rob.bat"

    result = objShell.Run(Chr(34) & cmd & Chr(34), 1, True)
    Set objShell = Nothing

End Sub

Open in new window

rrhandle8Asked:
Who is Participating?
 
Bill PrewCommented:
Makes sense, but not sure why you didn't get that error running the BAT.  Unless the BAT you ran then was in the same folder as the EXE.

At any rate, I'd say you have a few choices:
  1. Copy the EXE where the BAT will be.
  2. Add the folder where the EXE exists to the PATH
  3. Fully qualify the path to the EXE in the BAT
  4. In the BAT, add a line to CD to the directory where the EXE is before running it


»bp
0
 
NorieVBA ExpertCommented:
How is not working now?
0
 
rrhandle8Author Commented:
Line 13 or 15 is not doing anything.
The result.txt file does not change.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Bill PrewCommented:
The code looks okay, what is the exact contents of the Command defined range?


»bp
0
 
rrhandle8Author Commented:
swetest -p4 -b1.1.2017 -n4000 -s1 -g, -fTjPlsbd -head -true > results.txt

If I double-click on the .bat file which contains the above, everything works fine, but not if I run the macro.
The macro use to work fine.
The macro successfully updates the .bat file, but does not run the .bat file
Thank you
0
 
Bill PrewCommented:
Okay, I would approach it this way.

Add a PAUSE to the end of the BAT file (you will need to add that to the "Command" range so it gets written to the BAT file.  That way when it runs you can look at the DOS window for any messages.

Also, check the return code from the .Run() method, what does it contain after the execution of the BAT file?

Make sure there is not an "ON ERROR RESUME NEXT" active when the code runs, you want to see any VBA errors thrown.

Debug through the code line by line, check all variables as they are assigned to make sure they are what you are expecting.  And look at the command window when the PAUSE occurs to see if any errors are generated there.  Also make sure that it is running in the same current working directory as where the BAT file exists, it could be the results.txt file is getting written to a different folder than you think.


»bp
0
 
rrhandle8Author Commented:
Ah, there is a problem.  'swetest' is not recognized as an internal or external command, operable program or batch file.
0
 
rrhandle8Author Commented:
I tried everything you suggested. No help. Had to leave for awhile. Left computer running. Came back. It still did not work. I created another workbook. Did not work. Went back to the original workbook. IT WORKS AGAIN!? Don't have a clue what happened, but I thank you for your effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.