如何使用VBA执行shell命令?

时间:2010-02-18 16:45:41

标签: windows vba shell

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

C:\Temp\gc.exe 1

我该怎么做?

2 个答案:

答案 0 :(得分:11)

示例:

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

链接:Shell Invoked from VBA

答案 1 :(得分:2)

这显示了您想要调用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