Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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文件';Powershell中的宏错误?_Excel_Vba_Powershell - Fatal编程技术网

如何捕获Excel文件';Powershell中的宏错误?

如何捕获Excel文件';Powershell中的宏错误?,excel,vba,powershell,Excel,Vba,Powershell,我有一个计划的PowerShell脚本,可以打开Excel、打开文件并运行宏。 如果出现错误,我需要在shell中显示它并中断脚本。(我无法在等待用户输入时挂起脚本) 这是我的当前脚本($excelDataPath在脚本前面定义) 谢谢我认为从PowerShell的角度来看,这将捕获并显示已发生的异常: try { $Excel = New-Object -ComObject "Excel.Application" $Excel.DisplayAlerts = $false

我有一个计划的PowerShell脚本,可以打开Excel、打开文件并运行宏。 如果出现错误,我需要在shell中显示它并中断脚本。(我无法在等待用户输入时挂起脚本)

这是我的当前脚本($excelDataPath在脚本前面定义)


谢谢

我认为从PowerShell的角度来看,这将捕获并显示已发生的异常:

try {
    $Excel = New-Object -ComObject "Excel.Application"
    $Excel.DisplayAlerts = $false
    $Excel.AskToUpdateLinks = $false
    $Excel.Visible = $true

    $Workbook = $Excel.Workbooks.Open($excelDataPath)
    $Excel.Run("Macro1")
    $Workbook.Save()
    $Workbook.Close($true)
    $Excel.Close()
}
catch {
    $exception = $_.Exception
    while ($exception.InnerException) {
        $exception = $exception.InnerException
    }
    # Throw a terminating error. 
    throw $exception
}
finally {
    # IMPORTANT! Always release the comobjects used from memory when done
    if ($Workbook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Workbook) | Out-Null }
    if ($Excel)    { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null }
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

但是,对于您没有向我们展示的VBA宏代码,您必须检查它的功能,并通过任何错误。特别是
Err.Raise
Throw
语句。

我很遗憾不知道宏将是什么,因为这个脚本将用于自定义文件(可能是缺少文件、超出范围、VBA脚本中可能出现的任何错误)@SimonRosenfeld在这种情况下,恐怕您不能做更多。如果无法查看/编辑实际宏代码,则只能执行上述操作,因此PowerShell将排除异常。宏代码中处理(或不处理)了哪些异常,这是所有人的猜测。有没有办法至少不让脚本挂起?即使是试一试,我也只能在弹出窗口被点击时捕捉到。我不这么认为。。(VBA)弹出窗口显示什么?在将Excel设置为工作状态之前,您可以自己处理诸如“未找到文件”之类的简单错误,例如使用
测试路径-Path$excelDataPath-Path类型叶检查PowerShell中给定的
$excelDataPath
是否确实存在。
try {
    $Excel = New-Object -ComObject "Excel.Application"
    $Excel.DisplayAlerts = $false
    $Excel.AskToUpdateLinks = $false
    $Excel.Visible = $true

    $Workbook = $Excel.Workbooks.Open($excelDataPath)
    $Excel.Run("Macro1")
    $Workbook.Save()
    $Workbook.Close($true)
    $Excel.Close()
}
catch {
    $exception = $_.Exception
    while ($exception.InnerException) {
        $exception = $exception.InnerException
    }
    # Throw a terminating error. 
    throw $exception
}
finally {
    # IMPORTANT! Always release the comobjects used from memory when done
    if ($Workbook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Workbook) | Out-Null }
    if ($Excel)    { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null }
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}