VBA宏不执行.bat文件

时间:2018-07-13 17:00:47

标签: excel vba excel-vba

批处理文件可以手动执行,但是当我尝试从Excel中的宏执行时,在行Call Shell(stAppName, 1)上收到“运行时错误'5'无效的过程调用或参数”。这是我的代码:

Private Sub run_Click()
    Const DELIMITER As String = "," 'or "|", vbTab, etc.
    Dim myRecord As Range
    Dim myField As Range
    Dim nFileNum As Long
    Dim sOut As String

    nFileNum = FreeFile
    Open "C:\apps\Test.bat" For Output As #nFileNum
    For Each myRecord In Range("B20:B24")
        With myRecord
            For Each myField In Range(.Cells(1), _
                    Cells(.Row, Columns.Count).End(xlToLeft))
                sOut = sOut & DELIMITER & myField.Text
            Next myField
            Print #nFileNum, Mid(sOut, 2)
            sOut = Empty
        End With
    Next myRecord
    Close #nFileNum

Dim stAppName As String

stAppName = "cmd.exe /k c:\apps\test.bat"
Call Shell(stAppName, 1)

End Sub

1 个答案:

答案 0 :(得分:0)

您是否尝试过更改

stAppName = "cmd.exe /k c:\apps\test.bat"

收件人

 stAppName = "c:\apps\test.bat"