我怎样才能使用VBA执行一个shell命令?

我想要使​​用Visual Basic for Applications执行如下的shell命令。

C:\Temp\gc.exe 1 

我该怎么做?

Solutions Collecting From Web of "我怎样才能使用VBA执行一个shell命令?"

例:

  retVal = Shell("C:\Temp\gc.exe 1", vbNormalFocus) 

链接: Shell从VBA调用

这显示了你想要调用一个EXE文件并使用shell命令将一个参数传递给它的场景。 以下代码通过传递url作为参数来检查chrome.exe所在的文件夹,并从那里调用www.google.com(假设您安装了chrome):

 Public Sub Display_Google() Dim chromePath As String chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe" If FileExists(chromePath) Then Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus Else chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus End If End Sub 
 Public Function FileExists(ByVal FileName As String) As Boolean On Error Resume Next FileExists = Not CBool(GetAttr(FileName) And (vbDirectory Or vbVolume)) On Error GoTo 0 End Function