excel macro to execute windows run command?

Badassack

New Member
Joined
Feb 26, 2014
Messages
36
I found the following code here on these forums but it errors out .
I guess X is a variable that needs to be defined? Or is there another method to get the run command to come up?


Sub Macro1()x = Shell("cmd.exe /c C:\TestFolder\Book1.pdf", 1)End Sub</pre>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I also found this code but it errors out also, with "expected end of statement".

exec("cmd", "/c", "dir", "c:\\program files");
 
Upvote 0
The first code should have worked. What error message did you get?

The second code looks like part of a VBScript code, but it needs to be attached to another object, and VBScript is a different language.
 
Upvote 0
The first code should have worked. What error message did you get?

The second code looks like part of a VBScript code, but it needs to be attached to another object, and VBScript is a different language.

it highlights "X =" and says "variable not defined." I am using "option explicit" also.
 
Last edited:
Upvote 0
I tried using "dim x as long" and it seemed to work for a second, of course you don't know what I'm doing do you ? I'm trying to run a program like you do in the "RUN" box. It works every time in the "RUN" box. Can't seem to make it work with Shell command. take a look......

Dim x As Long
x = Shell("cmd.exe /c D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe, _
D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak, _
C:\starbound", 1)
 
Upvote 0
I tried using "dim x as long" and it seemed to work for a second, of course you don't know what I'm doing do you ? I'm trying to run a program like you do in the "RUN" box. It works every time in the "RUN" box. Can't seem to make it work with Shell command. take a look......

Dim x As Long
x = Shell("cmd.exe /c D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe, _
D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak, _
C:\starbound", 1)

Changed /c to /k and added "start" between /k and D:\ , now the problem is it can't find that directory. If it's not one thing it's another. sheesh.
 
Upvote 0
X as long or integer should work for the return from the Shell function.

The filepaths used in the string of arguments for the CMD contain spaces so they need to be enclosed by quotes. Also the commas shouldn't be necessary. Can you try the following and see how it goes?

Code:
Dim x As Long
    x = Shell("cmd.exe /c _
       ""D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe"" _
       ""D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak"" _
       C:\starbound", 1)
 
Upvote 0
X as long or integer should work for the return from the Shell function.

The filepaths used in the string of arguments for the CMD contain spaces so they need to be enclosed by quotes. Also the commas shouldn't be necessary. Can you try the following and see how it goes?

Code:
Dim x As Long
    x = Shell("cmd.exe /c _
       ""D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe"" _
       ""D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak"" _
       C:\starbound", 1)

I wrote it exactly as you described, it gave me an error on the first set of doublequotes. "expected: list seperator or)"
 
Upvote 0
It's not assembling the string quite right. Try;

Code:
Dim x As Long
    x = Shell("cmd.exe /c " & _
       """D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe"" " & _
       """D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak"" " & _
       "C:\starbound", 1)
 
Upvote 0
It's not assembling the string quite right. Try;

Code:
Dim x As Long
    x = Shell("cmd.exe /c " & _
       """D:\ssd programs\games\Steam\SteamApps\common\Starbound\win32\asset_unpacker.exe"" " & _
       """D:\ssd programs\games\Steam\SteamApps\common\Starbound\assets\packed.pak"" " & _
       "C:\starbound", 1)

Well at least we have a different error. " D:\ssd is not recognized as an internal or external command, operable program or batch file." Just for my sake I'll tell you whats supposed to happen. The first line starts up the unpacker, second line unpacks the .pak file, third line is the destination folder. So it's not three separate commands, but one long one. I'm sure you had that figured. As far as the error goes, I know this program works, I've used it many times.
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top