Run an external bat file from inside VBA

  • Thread starter RockyMountainHiker
  • Start date
R

RockyMountainHiker

I am trying to create a Metrics report in Excel 7. The data for the report is
in an MKS application History. MKS does not provide a way to collect the
history data other than using a command line interface.
I have manually typed in the command line and run it redirecting the result
to a text file. I have written a macro tha loads the text file into excel and
parses the data for the information I need.
I have to create the report weekly and would like to be able to run the
command line from the macro.
My approach is to create the command line in the macro, save as a text file
with the .bat extension, run the bat file and then load the result and parse
it for the data I need.
The piece I do not know how to do is execute the external file from the macro.

If there is a way to run a command line inside Excel that would work as well.

Thanks for any help.
 
M

MeistersingerVonNurnberg

Hi -

Try the shell() function, which I use sometimes like the following.

Sub run_proc()
on error resume next

dim rc, out_file
out_file = "c:\windows\temp\pgm.out"

rc = Shell("perl -S GetFileList.pl >" & out_file, vbNormalFocus)
if err then msgbox error$
if rc <= 0 then msgbox "other err"

' may want to chk w others on using the following
DoEvents( )

End Sub
 
R

RockyMountainHiker

Hi

Thanks, I modified it to run the command line directly after passing it the
CLI string and the Output file.
It works great

Sub run_proc(CLI As String, outFile As String)
On Error Resume Next

Dim rc As Variant

rc = Shell("cmd.exe /C" & CLI & " > " & outFile, vbHide)
If Err Then MsgBox Error$
If rc <= 0 Then MsgBox "other err"

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top