Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 尝试从VBA运行.bat脚本失败,无错误代码_Excel_Vba_Batch File - Fatal编程技术网

Excel 尝试从VBA运行.bat脚本失败,无错误代码

Excel 尝试从VBA运行.bat脚本失败,无错误代码,excel,vba,batch-file,Excel,Vba,Batch File,总结: 我正在尝试使用Excel VBA在Windows计算机上运行.bat脚本。我在网上找到了描述如何做这件事的参考资料,并且我遵循了其中包含的说明。但是,我的.bat文件没有运行 详情: My.bat文件(“test.bat”)包含以下代码: echo off echo This is a test... pause exit Sub testBatchScript() 'Print a message to the immediate window to confirm sub

总结: 我正在尝试使用Excel VBA在Windows计算机上运行.bat脚本。我在网上找到了描述如何做这件事的参考资料,并且我遵循了其中包含的说明。但是,我的.bat文件没有运行

详情: My.bat文件(“test.bat”)包含以下代码:

echo off
echo This is a test...
pause
exit
Sub testBatchScript()

    'Print a message to the immediate window to confirm subroutine execution
    Debug.Print "The subroutine is running."

    'Initialize a windows shell object
    Dim wsh As WshShell
    Set wsh = New WshShell

    'Construct the full path to the .bat file
    Dim fullPath As String
    fullPath = Chr(34) + "C:\Users\User1\Desktop\test.bat" + Chr(34)

    'Run the .bat file
    Dim eCode As Long
    eCode = wsh.Run(fullPath, waitonreturn:=True, windowstyle:=1)

    If eCode <> 0 Then
        MsgBox ("An error occurred.")
    End If

End Sub
它位于我的桌面上,路径如下:“C:\Users\User1\desktop”。当我双击这个.bat文件或从命令行调用它时,它显示了所需的行为。也就是说,它暂停并等待我按任意键

我正在尝试使用以下代码从Excel VBA运行相同的.bat文件:

echo off
echo This is a test...
pause
exit
Sub testBatchScript()

    'Print a message to the immediate window to confirm subroutine execution
    Debug.Print "The subroutine is running."

    'Initialize a windows shell object
    Dim wsh As WshShell
    Set wsh = New WshShell

    'Construct the full path to the .bat file
    Dim fullPath As String
    fullPath = Chr(34) + "C:\Users\User1\Desktop\test.bat" + Chr(34)

    'Run the .bat file
    Dim eCode As Long
    eCode = wsh.Run(fullPath, waitonreturn:=True, windowstyle:=1)

    If eCode <> 0 Then
        MsgBox ("An error occurred.")
    End If

End Sub
子testBatchScript()
'将消息打印到即时窗口以确认子例程的执行
“调试.打印”子例程正在运行
'初始化windows shell对象
将wsh暗显为WshShell
设置wsh=newwshshell
'构造.bat文件的完整路径
将完整路径设置为字符串
fullPath=Chr(34)+“C:\Users\User1\Desktop\test.bat”+Chr(34)
'运行.bat文件
暗淡的eCode如长
eCode=wsh.Run(fullPath,waitonreturn:=True,windowstyle:=1)
如果eCode为0,则
MsgBox(“发生错误”)
如果结束
端接头
当我运行这个子程序时,什么也没发生。不弹出CMD窗口,也不弹出指示错误的消息框。但是,我知道由于即时窗口中出现的消息,子例程正在运行

我不明白为什么这不起作用。更令人困惑的是,我能够在几周前成功地从VBA调用.bat脚本

有人有什么解释或建议吗

更新1: 根据@Dhamo在下面评论中的建议,我已使用错误处理更新了VBA代码:

Option Explicit

Sub testBatchScript()

    On Error GoTo EH:

    'Print a message to the immediate window to confirm subroutine execution
    Debug.Print "The subroutine is running."

    'Initialize a windows shell object
    Dim wsh As WshShell
    Set wsh = New WshShell

    'Construct the full path to the .bat file
    Dim fullPath As String
    fullPath = Chr(34) + "C:\Users\User1\Desktop\test.bat" + Chr(34)

    'Run the .bat file
    Dim eCode As Long
    eCode = wsh.Run(fullPath, waitonreturn:=True, windowstyle:=1)

    If eCode <> 0 Then
        MsgBox ("An error occurred.")
    End If

    Exit Sub

EH:
    MsgBox ("The error description is: " & Err.Description)

End Sub
选项显式
子testBatchScript()
错误转到EH时:
'将消息打印到即时窗口以确认子例程的执行
“调试.打印”子例程正在运行
'初始化windows shell对象
将wsh暗显为WshShell
设置wsh=newwshshell
'构造.bat文件的完整路径
将完整路径设置为字符串
fullPath=Chr(34)+“C:\Users\User1\Desktop\test.bat”+Chr(34)
'运行.bat文件
暗淡的eCode如长
eCode=wsh.Run(fullPath,waitonreturn:=True,windowstyle:=1)
如果eCode为0,则
MsgBox(“发生错误”)
如果结束
出口接头
呃,
MsgBox(“错误描述为:”&Err.description)
端接头
当我运行这段代码时,仍然没有发生任何事情

更新2: 今天早上重新启动计算机后,我再次运行了测试代码,这次我收到一条弹出消息:“错误描述是:权限被拒绝”。然后我再次运行代码,看看是否可以复制此错误消息,但没有弹出窗口!此外,无论我运行代码多少次,我都无法重新显示错误消息。看来你做了些什么


“权限被拒绝”消息表明这是一个安全问题。有人有解决问题的办法吗?

没有答案,但太长了,无法发表评论:

如果将以下内容放入文件中,请将其另存为test.vbs(而不是文本文档:如果不小心,记事本将创建test.vbs.txt):


(根据您的情况调整完整路径)。将其保存到桌面,然后单击图标。发生了什么?

是否添加了“Windows脚本主机对象模型”引用?如果还没有,请添加它并尝试[?==VBA窗口,单击工具>引用]@Dhamo:是的,我有。@Dhamo:对不起,我不理解“try[?==VBA窗口…”的意思。在第一行中添加显式选项,同时启用错误处理程序[on error goto EH:],并定义EH[EH:msgbox err.description]。至少您会在某种程度上了解实际问题。以上代码对我来说运行良好[我使用v2016]如果您已经添加了引用,您可以忽略进一步的注释,所以我想这意味着Excel是问题所在?有没有关于如何修复它的想法?您的猜测和我的一样好。我从未遇到过类似的情况。这几乎就像Excel在您看到它之前向命令窗口发送一个按键。我已更改了.bat文件,以便此时,它会创建一个单独的.txt文件,而不是等待击键。当我尝试从VBA调用.bat脚本时,不会创建此.txt文件。因此,在我可以看到窗口之前,Excel不会发送击键。这可能是windows安全“功能”。它可能不喜欢使用Excel->VBA->Bat文件,但它喜欢使用VBS->Bat文件。您是否也尝试过删除这些文件:
,waitonreturn:=True,windowstyle:=1
,并像使用VBS一样在Excel中使用
1,True
?是的,我担心这可能是一个安全问题……但是,如果是这样,为什么它似乎只影响我?