Welcome to Dream.In.Code
Become a VB Expert!

Join 149,507 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,347 people online right now. Registration is fast and FREE... Join Now!




calling .bat file from Excel

 
Reply to this topicStart new topic

calling .bat file from Excel, Trying to get .bat file successfully called from Excel

sjc
16 Aug, 2007 - 01:55 PM
Post #1

New D.I.C Head
*

Joined: 16 Aug, 2007
Posts: 2


My Contributions
Hello. This is my first post on DreamInCode. I've been programming for a while, but don't have any formal training in it, just what I've gotten from reading some books.

I'm trying to call a batch file from Excel. The batch file produces some other text files that I eventually want to read back into Excel. While the call to the batch file seems to work (a command window opens and a bunch of text flies by), the files that should be generated never appear. Here's my VBA code in Excel for calling the batch file:

CODE

Private Sub Run_Prog_Bttn_Click()
    Dim Test As Double
    Dim Response As VbMsgBoxResult
    Test = Shell("D:\TestRunforCall\TestRun.bat", vbNormalFocus)
    If Test = 0 Then
        Response = MsgBox("The Specified Batch File Does Not Exist.", vbOKOnly, "File Error")
        Exit Sub
    ElseIf Test > 0 Then
        Response = MsgBox("Successful call to Batch File.", vbOKOnly, "File Run Report")
    End If
End Sub


Everytime I click the button to call this Sub, the "Successful call to Batch File" message appears.

If I go to the folder where the batch file is located, I can double-click it and it runs fine, putting the files it generates into the folder with the batch file. So I know the contents of the batch file are correct. Something just isn't right with the Excel call.

Can anyone help me to understand why it's not working? I've looked all over and the files aren't being written anywhere else. They're just not being written at all.

Also, if it would be better to post this in one of the other forums, please let me know. After looking at their names, this one seemed to be the best place.

Thanks for any help you can share!

Sean

This post has been edited by sjc: 16 Aug, 2007 - 04:10 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Calling .bat File From Excel
16 Aug, 2007 - 04:15 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,481



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Try this,

vb

Private Sub Run_Prog_Bttn_Click()
Dim Test As Double
Dim Response As VbMsgBoxResult
Dim sYourCommand as String
sYourCommand = "D:\TestRunforCall\TestRun.bat"
Test = Shell("cmd /c " & sYourCommand, vbNormalFocus)
If Test = 0 Then
Response = MsgBox("The Specified Batch File Does Not Exist.", vbOKOnly, "File Error")
Exit Sub
ElseIf Test > 0 Then
Response = MsgBox("Successful call to Batch File.", vbOKOnly, "File Run Report")
End If
End Sub


Hope this helps smile.gif

Happy Coding!
User is online!Profile CardPM
+Quote Post

sjc
RE: Calling .bat File From Excel
6 Sep, 2007 - 11:38 AM
Post #3

New D.I.C Head
*

Joined: 16 Aug, 2007
Posts: 2


My Contributions
Thanks very much. That seems to help. I had to adjust the paths a bit too for the .bat file and the files it calls, but everything does work now. Thanks again.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Calling .bat File From Excel
6 Sep, 2007 - 01:31 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Psycho is good at what he does there is no doubt about it! He is the man! smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Calling .bat File From Excel
6 Sep, 2007 - 06:49 PM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,481



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(Martyr2 @ 6 Sep, 2007 - 02:31 PM) *

Psycho is good at what he does there is no doubt about it! He is the man! smile.gif


Why thanks Martyr2 blush.gif

Programming is my life smile.gif

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month