【问题标题】:How can I execute a shell command using VBA?如何使用 VBA 执行 shell 命令?
【发布时间】:2011-01-18 10:07:49
【问题描述】:

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

C:\Temp\gc.exe 1

我该怎么做?

【问题讨论】:

    标签: windows vba shell


    【解决方案1】:

    例子:

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

    链接:Shell Invoked from VBA

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 2014-09-19
        • 2015-01-03
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2015-02-13
        • 2023-03-18
        • 2018-10-06
        • 2016-05-01
        相关资源
        最近更新 更多