如何在Powershell中选择Excel范围



>我在函数中有以下代码:

function Excel2PPT{

param ($xslx, $targetsheet, $targetrange, $pptx, $targetslide, $npath)
$Excel = New-Object -ComObject Excel.Application
$workbook = $Excel.workbooks.open($xslx)
$Excel.Visible= $true
$worksheet = $workbook.worksheets.Item($targetsheet)
$range = $worksheet.Range($targetrange)
$range.select()
$range.copy()

我在另一个函数中使用它,该函数将"A1:O12"作为$targetrange传递给它

这在以前的测试中有效,但现在会产生错误和意外行为。

我将其称为:

Excel2PPT $IncomingABCExcel 1 'A1-O12' $testPPT2 $Targetslide $testPPT3

错误是:

Exception from HRESULT: 0x800A03EC
At C:...Excel2PPT.ps1:16 char:1
+ $range = $worksheet.Range($targetrange)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At C:...Excel2PPT.ps1:17 char:1
+ $range.select()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:...Excel2PPT.ps1:18 char:1
+ $range.copy()
+ ~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

我能做些什么来修复它?它应该是将$targetrange复制并粘贴到预期的PPT幻灯片上,但它似乎是将"$IncomingABCExcel"(不是变量,变量名称(复制并粘贴到幻灯片上。

您遇到的问题是您使用"A1-012"作为范围。这应该是"A1:012":

function Excel2PPT {
param ($xslx, $targetsheet, $targetrange, $pptx, $targetslide, $npath)
$Excel = New-Object -ComObject Excel.Application
$workbook = $Excel.workbooks.open($xslx)
$Excel.Visible= $true
$worksheet = $workbook.worksheets.Item($targetsheet)
$range = $worksheet.Range($targetrange)
$range.select()
$range.copy()
}
Excel2PPT $IncomingABCExcel 1 'A1:O12' $testPPT2 $Targetslide $testPPT3

最新更新