Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Running a Batch file from VBA

I am trying to run a batch file from Excell 2003 using the Shell function in
VBA and I cant seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch file
(e.g., D:\...\Batchfile.bat)

The line executes €“ I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the batch
file is supposed to produce. A search of my hard drives indicates that the
results have not been created in some unexpected location.

The batch file works properly when run manually.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Running a Batch file from VBA

In older version of Windows (I thought this was changed in newer versions,
but I haven't used Shell in ages, so I don't know for sure), you used to
have to encase any path/filenames where either the path or filename
contained internal spaces. You could try this and see if it works...

RetVal = Shell("""" & BatFilePath & """", vbNormalFocus)

Another possibility is that the briefly flashing command window is
displaying an error message of some kind. I believe this line of code will
leave the command window up so you can read its contents...

RetVal = Shell(Environ$("comspec") & " /k " & """" & _
BatFilePath & """", vbNormalFocus)

--
Rick (MVP - Excel)


"D.riggins" wrote in message
...
I am trying to run a batch file from Excell 2003 using the Shell function
in
VBA and I cant seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch
file
(e.g., D:\...\Batchfile.bat)

The line executes €“ I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the
batch
file is supposed to produce. A search of my hard drives indicates that
the
results have not been created in some unexpected location.

The batch file works properly when run manually.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Running a Batch file from VBA

Dim strSheet as String
strSheet = Sheets("info").Range("F30")
Sheets(strSheet).Activate

OR

Dim strSheet as String
strSheet = Sheets("info").Range("F30")
Application.Goto Reference:=Sheets(strSheet).Range("A1")

--
If this post helps click Yes
---------------
Jacob Skaria


"Rick Rothstein" wrote:

In older version of Windows (I thought this was changed in newer versions,
but I haven't used Shell in ages, so I don't know for sure), you used to
have to encase any path/filenames where either the path or filename
contained internal spaces. You could try this and see if it works...

RetVal = Shell("""" & BatFilePath & """", vbNormalFocus)

Another possibility is that the briefly flashing command window is
displaying an error message of some kind. I believe this line of code will
leave the command window up so you can read its contents...

RetVal = Shell(Environ$("comspec") & " /k " & """" & _
BatFilePath & """", vbNormalFocus)

--
Rick (MVP - Excel)


"D.riggins" wrote in message
...
I am trying to run a batch file from Excell 2003 using the Shell function
in
VBA and I cant seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch
file
(e.g., D:\...\Batchfile.bat)

The line executes €“ I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the
batch
file is supposed to produce. A search of my hard drives indicates that
the
results have not been created in some unexpected location.

The batch file works properly when run manually.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Running a Batch file from VBA

Oops ..the below is a wrong post; please ignore..
--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

Dim strSheet as String
strSheet = Sheets("info").Range("F30")
Sheets(strSheet).Activate

OR

Dim strSheet as String
strSheet = Sheets("info").Range("F30")
Application.Goto Reference:=Sheets(strSheet).Range("A1")

--
If this post helps click Yes
---------------
Jacob Skaria


"Rick Rothstein" wrote:

In older version of Windows (I thought this was changed in newer versions,
but I haven't used Shell in ages, so I don't know for sure), you used to
have to encase any path/filenames where either the path or filename
contained internal spaces. You could try this and see if it works...

RetVal = Shell("""" & BatFilePath & """", vbNormalFocus)

Another possibility is that the briefly flashing command window is
displaying an error message of some kind. I believe this line of code will
leave the command window up so you can read its contents...

RetVal = Shell(Environ$("comspec") & " /k " & """" & _
BatFilePath & """", vbNormalFocus)

--
Rick (MVP - Excel)


"D.riggins" wrote in message
...
I am trying to run a batch file from Excell 2003 using the Shell function
in
VBA and I cant seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch
file
(e.g., D:\...\Batchfile.bat)

The line executes €“ I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the
batch
file is supposed to produce. A search of my hard drives indicates that
the
results have not been created in some unexpected location.

The batch file works properly when run manually.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Running a Batch file from VBA

Keep in mind, a batch file isn't an executable file. When run from a
console (dos type) window or in the context of the Windows operating system
environment, it is "hosted" by either cmd.exe or command.com. The Shell
function doesn't check the Windows registry to determine the program needed
to launch a file with the extension "bat". The solution is to use the Shell
function to start either cmd.exe or command.com (or use %comspec% for more
versatility as in Rick's response earlier) and then present the full path to
your batch file as the argument to %comspec%.


Steve Yandl



"D.riggins" wrote in message
...
I am trying to run a batch file from Excell 2003 using the Shell function
in
VBA and I can't seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch
file
(e.g., D:\...\Batchfile.bat)

The line executes - I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the
batch
file is supposed to produce. A search of my hard drives indicates that
the
results have not been created in some unexpected location.

The batch file works properly when run manually.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Running a Batch file from VBA

Thanks. Having the command window stay open helped. The command window was,
in fact showing an error message. It turns out that the batch file was being
executed at the root of the D:\ drive and not in the subdirectory of the D
drive where the batch file is stored. As a result the batch file could not
find the other files it was supposed to process (the batch file simply
concatenates several MP3 files located in the same directory as the batch
file using the copy command).

For now I've taken the brute force approach and changed the batch file so it
explicitly changes the directory before processing the rest of the commands
in the batch file. I'm wondering, though, if there is not a more elegant
means of doing this using the Environ command or some other command.


"Rick Rothstein" wrote:

In older version of Windows (I thought this was changed in newer versions,
but I haven't used Shell in ages, so I don't know for sure), you used to
have to encase any path/filenames where either the path or filename
contained internal spaces. You could try this and see if it works...

RetVal = Shell("""" & BatFilePath & """", vbNormalFocus)

Another possibility is that the briefly flashing command window is
displaying an error message of some kind. I believe this line of code will
leave the command window up so you can read its contents...

RetVal = Shell(Environ$("comspec") & " /k " & """" & _
BatFilePath & """", vbNormalFocus)

--
Rick (MVP - Excel)


"D.riggins" wrote in message
...
I am trying to run a batch file from Excell 2003 using the Shell function
in
VBA and I cant seem to get it to work. The code is like this:

RetVal = Shell(BatFilePath, vbNormalFocus)

Where BatFilePath is a string variable with the full path of the batch
file
(e.g., D:\...\Batchfile.bat)

The line executes €“ I get a value in the RetVal variable and I see a brief
flash of the command prompt window, but I do not see the file that the
batch
file is supposed to produce. A search of my hard drives indicates that
the
results have not been created in some unexpected location.

The batch file works properly when run manually.



  #7   Report Post  
Posted to microsoft.public.excel.programming
PNK PNK is offline
external usenet poster
 
Posts: 1
Default Running a Batch file from VBA

My problem is identical to the one mentioned here and the advice about
keeping the window open definitely helped. Any suggestions as to how one can
change the starting directory of the shell or command prompt so that the
default directory is the local directory instead of the D:\ drive.

I'm a novice to programming and I'm sure I haven't used all the right
terminology but I hope I was able to convey my point. Any help would be most
Appreciated.

Regards,
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
batch file call from a macro - how ? and required batch format VB-rookie Excel Programming 4 September 6th 08 12:52 AM
running a batch file Mohan[_2_] Excel Programming 3 April 10th 04 10:48 PM
Running Excel as a batch job AND in the background GB[_3_] Excel Programming 0 August 30th 03 01:08 PM
Running a batch file from VB Graham Carter Excel Programming 3 August 29th 03 04:18 PM
Running a batch file from Excel VBA verizon Excel Programming 1 July 20th 03 09:27 AM


All times are GMT +1. The time now is 03:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"