Solved

pushd command in VB  macro

Posted on 2013-11-01
5
1,288 Views
Last Modified: 2013-11-02
Folks

using a VB macro,how do I run the following commands in one go?



C:\>CD C:\

C:\>pushd c \\servername\folder\

Z:\\servername\folder\test.bat

thank in advance!
0
Comment
Question by:rutgermons
  • 2
  • 2
5 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39618103
That commands are syntactically incorrect. What you mean is:
cd C:\
pushd \\servername\folder
test.bat

Open in new window

but even then the first line is superfluous (unless the batch file accesses relatives paths on drive C:, which would be very, ehm, dumb to do).
In VBS, you can use WScript.Shell.Run to execute batch commands in cmd.exe:
WScript.CreateObject("WScript.Shell").Run("cmd /c pushd \\servername\folder & test.bat")

Open in new window

You can provide a "window visibility option" as second parameter of the .Run, see http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx for details.
0
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39618112
The best way to do this is probably create a Batch file to carry all the commands.

Copy con vbrun.bat
c:
cd\
pushd c \\servername\folder\
call z:\\servername\folder\test.bat

Open in new window

use F6 to save the bat file.

in the VB Script,

Function VBRun()
dim shell
set shell=createobject("wscript.shell")
shell.run "VBrun.bat"
set shell=nothing
End Function

Open in new window

0
 

Author Comment

by:rutgermons
ID: 39618161
qlemo

i tried to out ur statement in the macro but it stats object is required? pardon my ignorance, could u advise how next?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39619321
"VB Macro", so you are in Visual Basic for Applications (that is, MS Office)?
Because it works great in VB Script. For VBA however we have to remove the WScript (and we can do the same in VBS, btw):
Sub test()
CreateObject("WScript.Shell").Run ("cmd /k pushd \\servername\folder & test.bat")
End Sub

Open in new window

I've changed /c to /k in cmd.exe for testing purposes - if there is an error message, you will be able to see it, as the cmd window will remain open. After testing you should change it back to /c, so the cmd window closes when finished.
0
 

Author Comment

by:rutgermons
ID: 39619544
Thank you Qlemo! this works perfectly well
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now