Running Shell commands

  • Thread starter Thread starter jonefer
  • Start date Start date
J

jonefer

Is it possible to call shell commands directly with VB?

For example... instead of calling a batch file that has no
regard for my Access application could I call these
commands directly?

Then I could have control over parts of the process.


The following is an example of what my batch file does
1)unzip some files, send them to the printer
2)open some rtf's in word
3)clean up the files after I've printed them in word
=========================================================
@echo off
if #%1#==## goto End

:Start
j:
cd "\Production Eligibility\Eligibility\Load Reports"
if exist ul*.?rpt del ul*.?rpt
if exist *.rtf del *.rtf


:Unzip
wzunzip -o -yp uha%1.zip

:prtTEXT
if exist ul*.?rpt nprint ul*.?rpt q=laser8100n nb

:prtRTF
if not exist *.rtf goto End

"C:\Program Files\Microsoft Office\Office\winword.exe"
pefus_~1.rtf pefus_~2.rtf pefus_~3.rtf
goto finishword

Office2k\Office\winword.exe" pefus_~1.rtf pefus_~2.rtf
pefus_~3.rtf

:FinishWord

echo Press any key to continue after printing the RTF files
pause > nul

:Cleanup
if exist ul*.?rpt del ul*.?rpt
====================================================
 
You certainly can call Shell from VBA - look in Help for the syntax. For
instance for the unzip, just build a command string like

strCommand = "wzunzip.exe -o -yp uha%1.zip"

note you need the .exe ending on the program name
then

Shell strCommand

will execute the command.
One thing to watch is that the VBA execution will continue with the next
command while the Sell process runs independently, so if you need the
results from the Shell before doing anything else, you must call the windows
API WaitForSingleProcess and get a response before continuing. Does similar
to your batch "if exist" statement.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Yes, You can.

retVal = Shell("PathToBatch")

If no error occurs then Shell >0.
Shell function runs programs in asynchro... way! It means:
the program You are trying to run - could NOT end before
MS Access start execute instructions after Shell.

Sorry, my language is...
 
That seems like it'll do the trick.
Can you just clarify what you mean by:

"you need the .exe ending on the program name"

--does this mean in addition to
"wzunzip.exe -o -yp uha%1.zip" ... I would need .exe
somewhere else?
 
No, what I meant was that whereas in a batch file you can call a program
just by the name without the extension, eg in your batch file you had:

wzunzip -o -yp uha%1.zip

But when doing this from VB with a Shell command, you use

wzunzip.exe -o -yp uha%1.zip


--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Back
Top