+ Reply to Thread
Results 1 to 4 of 4

VBA code to open and execute files from different directories in Command window

  1. #1
    Registered User
    Join Date
    09-26-2012
    Location
    San Francisco, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    VBA code to open and execute files from different directories in Command window

    I have the following problem that I hope someone knows the solution of.
    First, I open a command window from VBA by
    strPrgm0= "C:\WINDOWS\system32\cmd.exe"
    Call Shell(strPrgm0, 1)

    From the command window, I tested that the following command (I typed it in), which opens an Rscript and runs an R file from different directories, works:
    C:\somepath\>"c:\DirectoryA\Rscript.exe" "c:\DirectoryB\TestRun.r"

    My question is, how do I do the same (making the command line call) to execute the R run from VBA?

    Thanks a lot.

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Homeless. I am not kidding.
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,134

    Re: VBA code to open and execute files from different directories in Command window

    You might want to do everything within the Shell statement. Here is an example, to get the PC's IP Address use command, the code will look like this

    Call Shell("cmd.exe /k ipconfig ", vbNormalFocus) So in your case, perhaps, try Call Shell("cmd.exe /k C:\somepath\>"c:\DirectoryA\Rscript.exe" "c:\DirectoryB\TestRun.r"", vbNormalFocus)

  3. #3
    Registered User
    Join Date
    09-26-2012
    Location
    San Francisco, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA code to open and execute files from different directories in Command window

    Thanks. Those extra quotation marks seem to cause some problem...

  4. #4
    Registered User
    Join Date
    09-26-2012
    Location
    San Francisco, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA code to open and execute files from different directories in Command window

    I made it to work as follows;

    strPrgm = "C:\patha\Rscript.exe"
    strRFile = "c:\pathb\TestRun.r"
    strRFile = " " & Chr(34) & strRFile & Chr(34)
    Call Shell(strPrgm & strRFile, 1)

    Thanks for your suggestion.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1