# Microsoft Office Application Help - Excel Help forum > Excel Programming / VBA / Macros >  >  VBA Shell command

## sumanc_nitr

Hi Guys,

I had a problem using the shell command in VBA in Excel 2007.

I was trying to run the shell command in VBA (excel2007) on a .BAT(batch) file. While running the macro, the DOS command prompt appears and goes away in a flash but the function of the .bat is not performed which makes me think that the shell command isnt functioning properly. I had this proper earlier also with a few Exe's.

the line of the macro that calls is (c is the path for the batch file) :

Call Shell(c, vbNormalFocus)

Could any of you help me with some suggestions regarding this problem.

Just to confirm, the batch file on itself works absolutely fine and it looks like it doesnt take more than 2 seconds to run it. It also does not wait for any input from the macro. it is an individual batch just included in the macro.

Thank you.

----------


## Phil_V

Try this:




```
Please Login or Register  to view this content.
```


The chr(34)'s effectively put double quotes around the filename and path of the batch file, which is neccasary if either the path or the filename contain embedded spaces.

----------


## sumanc_nitr

Hi Phil,
Thanks for the reply. I tried everything that so that i dont come across these path problems. Now i'm trying to run an exe from C drive. The name of the exe contains a couple of words joined by underscores. I see the same problem. I run it from the command line it works fine. If i run it from a workbook macro it doesnt perform what it has to do.

I tried this.

program = Chr(34) + program + Chr(34)
taskid = Shell(program, vbNormalFocus)

It si the same problem. When i cross the shell command line in debug mode, the DOS command prompt appears and leaves in a flash but the exe does not perform what it has to do.

I would appreciate if you guys could provide further suggestions.

----------


## Phil_V

Well as far as I know there is nothing wrong with the way the shell command is being used there.

If you could create a sample workbook that exihibits the same behaviour, and then upload that then we might be able to help some more.

----------


## romperstomper

Does it make any difference if you use:



```
Please Login or Register  to view this content.
```

----------


## sumanc_nitr

Hi,

I guess i should try by using cmd.exe May be that could work. however i had a problem doing that. 

program = "C:\WINDOWS\SYSTEM32\cmd.exe C:\get_data.exe"

taskid = Shell(program, vbNormalFocus)

this thing just opens the command prompt and does not run the exe.

is there a way i could open the command prompt and run that exe in this manner. I guess there should be a change in the way i have written the program variable. Please let me know.

----------


## romperstomper

You could try the code I posted?

----------


## sumanc_nitr

I tried it. No luck with that.

----------


## romperstomper

What did c contain and what actually happened?

----------


## sumanc_nitr

hI,

Just to make things simpler. I have an exe by name get_data.exe in the C drive.
If i run this from the command line, it works fine. But it doesnt when i run it from the macro. the macro code i'm using is:



```
Please Login or Register  to view this content.
```


And when i run this macro the DOS prompt appears and disappears in a flash but the exe does not do what it is supposed to do.

What reasons do you see for this to happen?

----------


## sumanc_nitr

As a continuation to the previous post, if i try the same on calc.exe in a macro, it works fine.

----------


## Leith Ross

Hello sumanc_nitr,

To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time. 

*How to wrap your Code*
*1.* Select all your code using the mouse.
*2.* Click on the *#* icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post. 

*3.* You can also do this manually by placing the tags 
*[code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]* at the end of the line.

----------


## Leith Ross

Hello sumanc_nitr,

Your post is a little confusing. You state in your first post...




> I was trying to run the shell command in VBA (excel2007) on a .BAT(batch) file.



In post #10 you said...




> Just to make things simpler. I have an exe by name get_data.exe in the C drive.



1. Are you trying to run a ".bat" or ".exe" file? 
2. Do you want to want for the file to finish running before returning to Windows?

----------


## sumanc_nitr

Hi Leith,

Thanks for your reply. I wanted to make the problem simpler and hence replaced the .bat file with the .exe file. Actually, i wanted to run the exe from the .bat file. But just to make things simpler i'm using the .exe file from C:\ drive. I was suggested to use Chr(34) to avoid for spaces in the path. Just to get around the whole thing, ro the time being i'm trying to run the exe file from C:\ drive. The macro needs to just execute the exe. It does not have any latter code which depends on the exe to finish running. All the macro has to do is to run this exe.

----------


## sumanc_nitr

I think that the exe is being called from the command line, but is not able to run or finish running.

----------


## Leith Ross

Hello sumanc_nitr,

If your executable expects command line switches then those need to be included as well. Add them in after the file name.



```
Please Login or Register  to view this content.
```

----------


## sumanc_nitr

Hi Leith,

This also did not work for me. 




```
Please Login or Register  to view this content.
```


But when i type in the command prompt "C:\get_data.exe" and hit the return key, it works fine.

----------


## Phil_V

Ok, this is what I would do in this situation.
Create a Batch file on C:\

*C:\Test.bat:*



```
Please Login or Register  to view this content.
```


This simple batch file will run, should print "Hello World!" in the command window, and then pause, waiting for a key press, (that is the key bit!)

*In Excel:*



```
Please Login or Register  to view this content.
```


Then run the macro, and see what happens.
IF the batch file runs successfully then you should get the "Hello World!" message, and a prompt to press any key. When you do so the command window will close.

Presuming that all of the above works correctly then modify your batch file to this:

*C:\Test.bat:*



```
Please Login or Register  to view this content.
```


Then run your Excel macro again. This should at least let you see any error messages etc that are coming back from either the command environment, or the exe file itself.

----------


## romperstomper

You can also shell cmd with the /k switch to leave the command window open after it attempts to run your specified program. (just replace the /c in my earlier code)

----------


## sumanc_nitr

Hi Phil,

Thanks a lot!!! Your suggestion did help. I figured out that rather than running the exe from the shell command through the macro, using a batch file which takes the path to the exe contained folder works better. 

The batch file contains:




```
Please Login or Register  to view this content.
```


The macro contains:




```
Please Login or Register  to view this content.
```


This worked for me.  But i dont know why the earlier methods didnt work. I dont logically see why they should be different. please let me know if you guys get to know something.

Thank you all..

----------


## romperstomper

Does your batch file work without the cd "C:\" line? Does the .exe have an .ini or other settings file?

----------


## Phil_V

I'd go with RS's train of thought on this one. It does seem that the EXE file is reliant on the correct working directory being set first, which points to it having supporting files

----------


## sumanc_nitr

Hi,

it works if i use the below code in the .bat file




```
Please Login or Register  to view this content.
```


But it doesnt work if i use 




```
Please Login or Register  to view this content.
```

----------


## Phil_V

> Does the .exe have an .ini or other settings file?



and the response to this bit of RS's post?

----------


## sumanc_nitr

I know that it does not have a .ini, but it has other file settings

----------


## Phil_V

What do you mean by "other file settings" ?

----------


## romperstomper

That appears to be your answer then. The executable requires the current directory to be the one it is located in.

----------


## geek0

I guess this thread is old, but here in 2014 I run into the same problem but only on one PC of many.  From within VB in an Excel macro, the Shell command runs sucessfully on all PCs in our group except for one.  This one PC has same OS and Excel version as all the rest.   Macro permissions and User Account Control settings seem to be identical to the other PCs.  I use the very simple and normally sucessful code:



```
Please Login or Register  to view this content.
```


As found by member sumanc_nitr, I can run this batch file outside of VB on the "bad" PC either from command window or double click on it's name in Windows Explorer, so the problem is probably not related to the batch file or the application it starts.  Some particular setting on the OS or Excel in this one PC must be the culprit preventing Excel VB from Shelling but we can't find it.  Any ideas will be appreciated.

Thanks,

----------


## arlu1201

Geek0,

Welcome to the forum. 

Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do.  Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.

----------

