Solved

pushd command in VB  macro

Posted on 2013-11-01
5
1,398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 69

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 69

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

726 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