excel - HRESULT : 0x800A03EC when setting value of a cell (through Powershell) 的异常

标签 excel powershell excel-automation

我正在尝试通过 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 = ...

我可以注释掉将单元格的值设置为公式的任意数量的行,第一个尝试这样做的行将抛出上述错误。

就像我说的,我以前运行过这个脚本,但现在它给我带来了麻烦。我怎样才能解决这个问题,使代码运行顺利?

最佳答案

将解决方案从一个问题移到另一个答案:

RESOLUTION:

To resolve this issue, in the text I assigned to the cell, I replaced single quotes with double quotes, and because of this, had to escape the '$' and ' " ' characters. I also ADDED single quotes around "Report" (The table name from which data is being pulled) each time it came up within the text.

It ended up looking like this, and running fine:

$StatisticsSheet.Cells.Item($row,$column) = "=COUNTIFS('Report'!E2:E200000,B`$3,'Report'!G2:G200000,`"UserMailbox`")"

Still not sure why this error occurred-the code had worked every week prior to this! Before changing the script and finding the resolution, I tested it on multiple machines (5+) and it threw the error mentioned in the title every single time.

关于excel - HRESULT : 0x800A03EC when setting value of a cell (through Powershell) 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45040461/

相关文章:

vb.net - 从VB.NET应用程序调用Excel时,如何使Excel显示在应用程序前面?

c# - 在 Excel 中使用 VSTO 创建 UDF

c# - 如何将 Epplus 与包含几行的单元格一起使用

string - Powershell字符串到数字功能?

powershell - 如何在 PowerShell 提示符下(不在脚本中)初始化多行数组?

python - openpyxl - "copy/paste"单元格范围

VBA 脚本来计算字符串、插入行、复制行、拆分单元格

VBA Excel-基于具有匹配数据的两列复制行

powershell - 如何在powershell中检查关联数组是否为空

excel - 如何在使用 TextToColumns 时将所有列转换为文本,默认情况下为常规范式 - 使用 Powershell