?
Solved

pushd command in VB  macro

Posted on 2013-11-01
5
Medium Priority
?
1,494 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 70

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 70

Accepted Solution

by:
Qlemo earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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