首页 > 解决方案 > 从 Excel 自定义函数返回到 Powershell

问题描述

我无法将 Excel 文档中的 VBA 函数结果检索到 Powershell 脚本中。

Powershell 文件

$suite = "<some_path>"
$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open($suite)

$specs = $excel.Run("ThisWorkbook.ftp_specs")
IF([string]::IsNullOrEmpty($specs)) {            
    Write-Host "Your string is EMPTY or NULL"           
} else {            
    Write-Host "Your string is not EMPTY"           
}

Excel 文件(在 ThisWorkbook 中)

Function ftp_specs()
    ftp_specs = "hello"
End Function

Powershell 运行结果 Your string is EMPTY or NULL

MS Office VBA 参考声明Application.Run返回一个 Variant。

在过去的几个小时里,我在网上看到的任何东西都没有暗示任何相关的东西。

标签: excelvbapowershell

解决方案


您不需要ThisWorkbook (?) 命名空间。只需使用Run带有函数名称的方法。像这样:

$specs = $excel.Run("ftp_specs")

推荐阅读