macrohappy
asked on
I am having trouble opening a .bat file in a VBA Excel Macro
I am using the following code to open a .bat file in a Excel VBA macro.
Sub OpenFile()
Dim RetVal
RetVal = Shell("C:\MyMonitor\Monito r\startMon itorLinux. bat", 1)
End Sub
The code opens the command window but I get a path not recognized error. If I go to the file directly and double click it the file opens without a problem. Please help
FYI I am still learning VB so please keep any responses in laymens terms
Thanks
Sub OpenFile()
Dim RetVal
RetVal = Shell("C:\MyMonitor\Monito
End Sub
The code opens the command window but I get a path not recognized error. If I go to the file directly and double click it the file opens without a problem. Please help
FYI I am still learning VB so please keep any responses in laymens terms
Thanks
try this...
RetVal = Shell("cmd /C C:\MyMonitor\Monitor\start MonitorLin ux.bat", 1)
RetVal = Shell("cmd /C C:\MyMonitor\Monitor\start
I tried it in Execel VBA macro with my bat file. It works fine finally
But it need to seperate cmd with bat file name by "cmd /c " & " C:\.......\example.bat "
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "c:\mydir\example.bat", 1)
End Sub
Hope you understand
But it need to seperate cmd with bat file name by "cmd /c " & " C:\.......\example.bat "
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "c:\mydir\example.bat", 1)
End Sub
Hope you understand
I tried the my previouse one it also work too so now you can use either one
'MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
'Method-2
Sub OpenFile()
Shell "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", vbMaximizedFocus ' open a txt document
End Sub
'MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star
End Sub
'Method-2
Sub OpenFile()
Shell "C:\MyMonitor\Monitor\star
End Sub
If you want to open or edit your bat file in editor instead of command running the bat file, you can
try this and it works fine also.
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
try this and it works fine also.
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star
End Sub
ASKER
I have tried all of the above solutions and no luck. After futher research it looks like the batch files do run however the program that they are suppose to open doesn't open. See command window info attached
'\\us\home\d\joe.smith'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\WINDOWS>set JP=C:\CameronMonitor\Monitor\lib\fix
C:\WINDOWS>lib\jre\bin\java -mx256M -classpath "C:\CameronMonitor\Monitor\lib\fi
x\fixFilePersistence.jar;C:\Monitor\Monitor\lib\fix\fixCore.jar;C:\
Monitor\Monitor\lib\fix\fixMonitorGuiNew.jar;C:\Monitor\Monitor\lib\fix\
fixMonitoring.jar;C:\Monitor\Monitor\lib\fix\fixUniversalServer.jar;" com
.systems.fix.monitor.MonitorApplication -monitor //s0432cdc.cdc.schwab.co
m:3000/MonitorServer
The system cannot find the path specified.
C:\WINDOWS>pause
Press any key to continue . . .
Actually, your preivous question is passed for the running/opening bat file in VBA using shell ? NOw the new
question is in bat file script command inside doesn't work and windows can't find the path This is other
issue/queustion , RIght ? You should seperate the question, otherwise we get confused
question is in bat file script command inside doesn't work and windows can't find the path This is other
issue/queustion , RIght ? You should seperate the question, otherwise we get confused
ASKER
I apologize for the confusion, the vba code:
RetVal = Shell("cmd /C" & "C:\CameronMonitor\Monitor \startMoni torLinux.b at", 1)
returns the output that was attached I am not really sure if the batch is actually running. When I run C:\CameronMonitor\Monitor\ startMonit orLinux.ba t from RUN it opens the program fine
RetVal = Shell("cmd /C" & "C:\CameronMonitor\Monitor
returns the output that was attached I am not really sure if the batch is actually running. When I run C:\CameronMonitor\Monitor\
Actually, it is running, I did it at my side there is no issue. Try to separate two question, You cann other new bat file
with just one simple comand such as notepad.exe without other commands. IF notepad window is active, there should be no problem, VBA code with shell comand coding , but the new problem might be the java command in your orignal bat file. That is other new issue
with just one simple comand such as notepad.exe without other commands. IF notepad window is active, there should be no problem, VBA code with shell comand coding , but the new problem might be the java command in your orignal bat file. That is other new issue
Sorry Typing mistake for previous email
To narrow down the proble, try to replace the orginal bat file with new bat file with just notepad.exe include
Actually, it is running, I did it at my side there is no issue. Try to separate two question, You can replace other new bat file
with just one simple comand such as notepad.exe without other commands. If notepad window is active, there should be no problem, VBA code with shell comand coding , but the new problem might be the java command in your orignal bat file. That is other new issue
To narrow down the proble, try to replace the orginal bat file with new bat file with just notepad.exe include
Actually, it is running, I did it at my side there is no issue. Try to separate two question, You can replace other new bat file
with just one simple comand such as notepad.exe without other commands. If notepad window is active, there should be no problem, VBA code with shell comand coding , but the new problem might be the java command in your orignal bat file. That is other new issue
ASKER
when I run:
RetVal = Shell("cmd /C" & "notepad.exe", 1)
I get a similar message
'\\us\home\d\joe.smith'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
I hope this is what you are asking?
RetVal = Shell("cmd /C" & "notepad.exe", 1)
I get a similar message
'\\us\home\d\joe.smith'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
I hope this is what you are asking?
ASKER
Adding C:\ works
RetVal = Shell("cmd /C" & "C:\notepad.exe", 1)
Actually, you want to run bat file or open bat file in Shell compand ?
If running bat file ,using method-1. If opening bat file, using method-2 with notepad open file command
'MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
'Method-2
Sub OpenFile()
Shell "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", vbMaximizedFocus ' open a txt document
End Sub
If there is no issue, the new issue it is the commands inside your bat file not the shell and VBD coding problem
Please clarify this before going next step
If running bat file ,using method-1. If opening bat file, using method-2 with notepad open file command
'MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star
End Sub
'Method-2
Sub OpenFile()
Shell "C:\MyMonitor\Monitor\star
End Sub
If there is no issue, the new issue it is the commands inside your bat file not the shell and VBD coding problem
Please clarify this before going next step
Sorry again, cut and paste mistake. Now, it is corret Method-1, opeing bat file, method-2 is opeing bat file using notepad
MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
'Method-2
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
MEthod-1
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star
End Sub
'Method-2
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star
End Sub
ASKER
What I am not understanding is why the batch file opens correctly when double clicking on it or running it directly from Run but does open properly when opening it from the Shell? Doesn't that suggect that the shell is not opening the file the same way as simply double clicking on it?
Sorry Sorry again, cut and paste mistake and typing too fast . Now, it is corret Method-1, running bat file , method-2 is opening bat file using notepad
Method-1(running bat file and its command inside the bat file)
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
'Method-2(opening bat file by notepad editor)
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
End Sub
Method-1(running bat file and its command inside the bat file)
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star
End Sub
'Method-2(opening bat file by notepad editor)
Sub OpenFile()
Dim RetVal
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star
End Sub
ASKER
When I run
RetVal = Shell("NOTEPAD.EXE" & "C:\MyMonitor\Monitor\star
it returns runtime error 53. file not found
it might be different version of VB/Excel issue
Please try this
RetVal = Shell("NOTEPAD.EXE C:\MyMonitor\Monitor\start MonitorLin ux.bat", 1)
Please try this
RetVal = Shell("NOTEPAD.EXE C:\MyMonitor\Monitor\start
ASKER
On Notepad it returns...
@echo On
set JP=C:\MyMonitor\Monitor\li b\fix
lib\jre\bin\java -mx256M -classpath "%JP%\fixFilePersistence.j ar;%JP%\fi xCore.jar; %JP%\fixMo nitorGuiNe w.jar;%JP% \fixMonito ring.jar;% JP%\fixUni versalServ er.jar;" com.systems.fix.monitor.Mo nitorAppli cation -monitor //s0432cdc.cdc.schwab.com: 3000/Monit orServer
pause
exit
@echo On
set JP=C:\MyMonitor\Monitor\li
lib\jre\bin\java -mx256M -classpath "%JP%\fixFilePersistence.j
pause
exit
What you mean ? Okay or not okay ?
ASKER
It didn't start the program it just displayed the Notepad as seen in my previous comment. The only thing different it that it didn't pause for me to see the cmd box
Actually, you want editing the abt file or running file ?
At the begining of your question, you mention "I am having trouble opening a .bat file in a VBA Excel Macro"
Openign mean Editing, Right ?
And you say start the program, start what program ? please clarify, otherwise I don't see you problem
At the begining of your question, you mention "I am having trouble opening a .bat file in a VBA Excel Macro"
Openign mean Editing, Right ?
And you say start the program, start what program ? please clarify, otherwise I don't see you problem
ASKER
OK, I am trying to start a batch job that executes a series of instructions that result in running a program called MyMonitor. I do not want to edit the file.
No different that using a Shell to launch a .EXE file
I really apologize if I am not communicating clearly
No different that using a Shell to launch a .EXE file
I really apologize if I am not communicating clearly
So, you should use method-1, using bat file to run program command inside that is not related to Notepad.exe
Notepad.exe is just a editor only.
Method-1(running bat file and its command inside the bat file)
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star tMonitorLi nux.bat", 1)
ANd you could send me your bat.file to check if you like and the error message from Method-1
End Sub
Notepad.exe is just a editor only.
Method-1(running bat file and its command inside the bat file)
Sub OpenFile()
Dim RetVal
RetVal = Shell("cmd /c " & "C:\MyMonitor\Monitor\star
ANd you could send me your bat.file to check if you like and the error message from Method-1
End Sub
ASKER
Here is the error message.
'\\us\home\d\joe.smith'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\WINDOWS>set JP=C:\MyMonitor\Monitor\li b\fix
C:\WINDOWS>lib\jre\bin\jav a -mx256M -classpath "C:\MyMonitor\Monitor\lib\ fi
x\fixFilePersistence.jar;C :\MyMonito r\Monitor\ lib\fix\fi xCore.jar; C:\
MyMonitor\Monitor\lib\fix\ fixMonitor GuiNew.jar ;C:\MyMoni tor\Monito r\lib\fix\
fixMonitoring.jar;C:\MyMon itor\Monit or\lib\fix \fixUniver salServer. jar;" com
.systems.fix.monitor.Monit orApplicat ion -monitor //s0432cdc.cdc.comp.co
m:3000/MonitorServer
The system cannot find the path specified.
C:\WINDOWS>pause
Press any key to continue . . .
'\\us\home\d\joe.smith'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\WINDOWS>set JP=C:\MyMonitor\Monitor\li
C:\WINDOWS>lib\jre\bin\jav
x\fixFilePersistence.jar;C
MyMonitor\Monitor\lib\fix\
fixMonitoring.jar;C:\MyMon
.systems.fix.monitor.Monit
m:3000/MonitorServer
The system cannot find the path specified.
C:\WINDOWS>pause
Press any key to continue . . .
DId you set correct path in your directory to link and run your java program
lib\jre\bin\java that dosen't seem the exact location
Step-1, Find out where is the location of your java program ,which directory
Step-2, set path=C:\program files\java\jre\bin\;C:\oth erdire; C:\other otherdir that is helping window to look for java
Step-3, change to java -mx256M -classpath "C:\MyMonitor\Monitor\lib\ fi
x\fixFilePersistence.jar;C :\MyMonito r\Monitor\ lib\fix\fi xCore.jar; C:\
MyMonitor\Monitor\lib\fix\ fixMonitor GuiNew.jar ;C:\MyMoni tor\Monito r\lib\fix\
fixMonitoring.jar;C:\MyMon itor\Monit or\lib\fix \fixUniver salServer. jar;" com
.systems.fix.monitor.Monit orApplicat ion -monitor //s0432cdc.cdc.comp.co
m:3000/MonitorServer in bat file instead of lib\jre\bin\java mx256M -classpath "C:\MyMonitor\Monitor\lib\ fi........ ....
Or you can change path in computer or document system option/property, you will see how to change path variable
Just set path directory issue, windows could not find where is java ...
lib\jre\bin\java that dosen't seem the exact location
Step-1, Find out where is the location of your java program ,which directory
Step-2, set path=C:\program files\java\jre\bin\;C:\oth
Step-3, change to java -mx256M -classpath "C:\MyMonitor\Monitor\lib\
x\fixFilePersistence.jar;C
MyMonitor\Monitor\lib\fix\
fixMonitoring.jar;C:\MyMon
.systems.fix.monitor.Monit
m:3000/MonitorServer in bat file instead of lib\jre\bin\java mx256M -classpath "C:\MyMonitor\Monitor\lib\
Or you can change path in computer or document system option/property, you will see how to change path variable
Just set path directory issue, windows could not find where is java ...
At command line C:
Just type as follows
C:\ set path
You will see all path you include in your system
Just type as follows
C:\ set path
You will see all path you include in your system
You might just need to add a line to your bat file to change to the directory where the lib folder can be found...
between the lines
Set JP= etc...... and
lib\jre\bin\java etc.....
make it:
Set JP= etc......
cd c:\MyMonitor\Monitor
lib\jre\bin\java etc.....
ASKER
I tried:
Set JP= etc......
cd c:\MyMonitor\Monitor
lib\jreC:\CameronMonitor\M onitor\\bi n\java etc.....
But it doesn't change the directly:
'\\us\home\d\joe.smith
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\WINDOWS>set JP=C:\MyMonitor\Monitor\li b\fix
C:\WINDOWS>cd C:\MyMonitor\Monitor\lib\j re\bin\jav a -mx256M -classpath "C:\
MyMonitor\Monitor\lib\fix\ fixFilePer sistence.j ar;C:\MyMo nitor\Moni tor\
lib\fix\fixCore.jar;C:\MyM onitor\Mon itor\lib\f ix\fixMoni torGuiNew. jar;C:\
MyMonitor\Monitor\lib\fix\ fixMonitor ing.jar;C: \MyMonitor \Monitor\l ib\fix
\fixUniversalServer.jar;" com.systems.fix.monitor.Mo nitorAppli cation -mon
itor //s0432cdc.cdc.comp.com:30 00/Monitor Server
The system cannot find the path specified.
C:\WINDOWS>pause
Press any key to continue . . .
This is what the instructions look like when the bat file is just double clicked on:
C:\MyMonitor\Monitor>set JP=C:\MyMonitor\Monitor\li b\fix
C:\MyMonitor\Monitor>lib\j re\bin\jav a -mx256M -classpath "C:\MyMonitor
\Monitor\lib\fix\fixFilePe rsistence. jar;C:\MyM onitor\Mon itor\lib\f ix\fixCor
e.jar;C:\MyMonitor\Monitor \lib\fix\f ixMonitorG uiNew.jar; C:\MyMonit or\M
onitor\lib\fix\fixMonitori ng.jar;C:\ MyMonitor\ Monitor\li b\fix\fixU niversalS
erver.jar;" com.systems.fix.monitor.Mo nitorAppli cation -monitor //s0432cd
c.cdc.comp.com:3000/Monito rServer
Notice that:
\\us\home\d\joe.smith
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
doesn't appear when the file is just opened directly. Could that be where the problem is.
Set JP= etc......
cd c:\MyMonitor\Monitor
lib\jreC:\CameronMonitor\M
But it doesn't change the directly:
'\\us\home\d\joe.smith
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\WINDOWS>set JP=C:\MyMonitor\Monitor\li
C:\WINDOWS>cd C:\MyMonitor\Monitor\lib\j
MyMonitor\Monitor\lib\fix\
lib\fix\fixCore.jar;C:\MyM
MyMonitor\Monitor\lib\fix\
\fixUniversalServer.jar;" com.systems.fix.monitor.Mo
itor //s0432cdc.cdc.comp.com:30
The system cannot find the path specified.
C:\WINDOWS>pause
Press any key to continue . . .
This is what the instructions look like when the bat file is just double clicked on:
C:\MyMonitor\Monitor>set JP=C:\MyMonitor\Monitor\li
C:\MyMonitor\Monitor>lib\j
\Monitor\lib\fix\fixFilePe
e.jar;C:\MyMonitor\Monitor
onitor\lib\fix\fixMonitori
erver.jar;" com.systems.fix.monitor.Mo
c.cdc.comp.com:3000/Monito
Notice that:
\\us\home\d\joe.smith
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
doesn't appear when the file is just opened directly. Could that be where the problem is.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got it.. I just used chdir.. instead of CD
set JP=C:\MyMonitor\Monitor\li b\fix
chdir..
C:\MyMonitor\Monitor\lib\j re\bin\jav a
set JP=C:\MyMonitor\Monitor\li
chdir..
C:\MyMonitor\Monitor\lib\j
Shell "c:\.......\yourfilename.t