【发布时间】:2017-07-11 16:53:12
【问题描述】:
我正在尝试通过 Powershell 为 excel 单元格设置一个值,但我收到错误 HRESULT: 0x800A03EC,这会提前结束脚本。我意识到还有其他与此错误或类似错误相关的问题,但没有一个解决方案对我有用,所以我假设这是一个单独的问题。
我之前运行过我的脚本,但它现在才给我这个错误。
相关代码:
$Output_Location = "Z:\Documents\Powershell"
$Excel_File = "Report.xlsx"
$ExcelWorkBook = $Excel.Workbooks.open("$Output_Location\$Excel_File")
$MainSheet = $ExcelWorkBook.worksheets.Item("Report")
$Sheet1 = $ExcelWorkBook.worksheets.Item("Sheet1")
$Sheet1.name = "Statistics"
$StatisticsSheet = $ExcelWorkBook.worksheets.Item("Statistics")
$row = 3
$column = 2
$StatisticsSheet.Cells.Item(2,2)= 'KeyToMatch'
$StatisticsSheet.Cells.Item($row,$column) = '=COUNTIFS(Report!E2:E200000,B$3,Report!G2:G200000,"UserMailbox")'
$row++
$StatisticsSheet.Cells.Item($row,$column) = '=COUNTIFS(Report!E2:E200000,B$3,Report!G2:G200000,"RemoteUserMailbox")'
$row++
代码加载 excel 文件并点击将单元格 (2,2)/(B,2) 设置为其值的行。但是,当代码在 KeyToMatch (B,3) 下方的行中设置单元格值时,会引发错误 0x800A03EC。
完全错误:
Exception from HRESULT: 0x800A03EC
At Z:\Documents\Powershell\Reporting\Report.ps1:113 char:1
+ $StatisticsSheet.Cells.Item($row,$column).value = '=COUNTIFS(Report! ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我尝试在单元格和值之间隔开“=”,我还尝试了以下操作:
$StatisticsSheet.Cells.Item($row,$column).value = ...
$StatisticsSheet.Cells.Item($row,$column).value2 = ...
$StatisticsSheet.Cells.Item($row,$column).text = ...
$StatisticsSheet.Cells.Item($row,$column).formula = ...
我可以注释掉将单元格的值设置为公式的任意数量的行,第一个尝试这样做的行将抛出上述错误。
就像我说的,我以前运行过这个脚本,但现在它给我带来了麻烦。我该如何解决这个问题,让代码运行顺畅?
【问题讨论】:
-
您的代码在 PowerShell 5.1 和 Excel 2013 上运行良好。您使用的是什么版本?你签出this post了吗?我看到您的代码中有
.xlsx,但这不一定是文件格式。尝试使用E2:E2000的范围来查看是否发生相同的错误。此外,$Excel.Visible = $true可能有助于调试。 -
@gms0ulman 我使用的是 Powershell 5.0.10586.117 和 Excel 2013。当我在(未完成的)自动 excel 文件(程序退出后)上选择属性时,它显示文件类型为 .xlsx。我使用 $Excel.visible = $true 但没有包括它以在我的问题中保存混乱 - 这就是我知道程序部分完成自动化的方式。我按照您链接的问题中的步骤进行操作,但再次测试时得到了相同的结果。知道为什么它会在我的系统上引发此错误吗?
-
@gms0ulman 我用范围 E2:E2000 进行了测试,它在同一行上抛出了同样的错误。
标签: excel powershell excel-automation