来自HRESULT的PowerShell异常:0x800A03EC

时间:2018-01-12 12:16:41

标签: excel powershell

欢迎任何帮助。 我使用PowerShell来操作Excel工作表。出于某种原因,这有效。

contentView

但这不是

    for($o = 1; $o -le $firstRowMes.Length + $dagenInDeMaand; $o++){
if($o -ne $exception)
{

    $ws.Cells.Item($o, 3) = $firstRowMes[$index]
    $ws.Cells.Item($o, 4) = $betweenMes[$index]
    $ws.Cells.Item($o, 5) = $Lastmes[$index]
    $ws.Cells.Item($o, 7) = "=C" + $o + "/F" + $o
    $ws.Cells.Item($o, 6) = "=SUM(C" + $o + ":E" + $o + ")"
    $index++
}
else
{
    $exception = ($exception + $applicationArray.Length +1)
}
}

如果我直接将输出插入excel,它可以正常工作。 我得到的完整错误是:

  

HRESULT的异常:0x800A03EC在   C:\ Users \ yniasr \ Documents \ Argenta \ Autoreportdagen.ps1:202 char:5
  + $ ws.Cells.Item($ index,11).Formula =" = AVERAGE(" + $ begintrendGet ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
      + CategoryInfo:OperationStopped:(:) [],COMException
      + FullyQualifiedErrorId:System.Runtime.InteropServices.COMException

我在这里看到了一些关于它的帖子,但他们的解决方案都没有适用于我的代码。

非常感谢!

1 个答案:

答案 0 :(得分:1)

我怀疑是因为公式中的;落后了。尝试使用$begintrendGetallen.trim(";")

您的索引初始化应该在循环之外。否则,只要循环运行,它就会重新设置为2

$index = 2
for($g = 1; $g -le $applicationArray.Length;$g++){

    $begintrendGetallen = ""

    for($i = 0; $i -le $eerstePeriode-1; $i++){
       $begintrendGetallen = $begintrendGetallen +  "G" + (1+$g+(9*$i)).ToString() + ";"    
    }

    $begintrendGetallen = $begintrendGetallen.trim(";")

    $ws.Cells.Item($index, 11).Formula = "=AVERAGE(" + $begintrendGetallen + ")"
    $index++
}
处理PowerShell / Excel时,

$i=0很不稳定,因为使用非零数组经常遇到Excel问题。我认为它在你的代码中没有引起任何问题。

相关问题