在VBA错误的目录中运行R脚本

我有一个编辑R脚本的macros。 那么R脚本应该被下面的VBA调用:

Dim shell As Object Set shell = VBA.CreateObject("WScript.Shell") Dim waitTillComplete As Boolean: waitTillComplete = True Dim style As Integer: style = 1 Dim errorCode As Integer Dim path As String path = """" & "C:\Program Files\R\R-3.3.2\bin\i386\R.exe" & """ """ & "R RAM Cluster Script.R" & """" errorCode = shell.Run(path, style, waitTillComplete) 

上面的代码是从这个问题 。

但是,当我执行macros时,R命令行给了我一个错误,指出:

  '\\dm\home\myaccount\*Path of my original Excel File*' "CMD.EXE was started with the above path as the current directory. UNC Paths are not supported. Defaulting to Windows directory. Argument 'R RAM Cluster Script.R' Ignored" 

该脚本存储在我的Excel工作簿所在的文件夹中。

任何人都可以帮我find我的问题吗?

尝试这个:

 Sub test() Dim shell As Object Set shell = VBA.CreateObject("WScript.Shell") Dim waitTillComplete As Boolean: waitTillComplete = True Dim style As Integer: style = 1 Dim errorCode As Integer Dim path As String path = """C:\Program Files\R\R-3.3.1\bin\RScript.exe""" & """c:/temp/R RAM Cluster Script.R""" errorCode = shell.Run(path, style, waitTillComplete) End Sub 

我会build议尝试使用powershell作为脚本的包装,因为它支持UNCpath。 所以,这样的事情应该工作:

 path = "Powershell.exe -executionpolicy bypass -Command &{'C:\Program Files\R\R-3.3.2\bin\i386\R.exe " & ThisWorkbook.Path & "\R RAM Cluster Script.R'}"