Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Basic excel script: HELP

$
0
0

I want a script to write to excel. But when I start with the basic it won't work.

I'm running this on Win7.

I have looked for hours, and tried diffrend commands, but result is always the same.
Can't write to cell and can't save excel file.

Script:

$xl=New-Object -ComObject Excel.Application
$wb=$xl.Workbooks.Add
$ws=$wb.ActiveWorkbook

$cells=$ws.Cells
$cells.Item(1,1).Value() = 'x' 
#$ws.cells.Item(1,1).Value() = "x" 

$xlWorkbookNormal = -4143 
$wb.SaveAs("H:\xlstest.xlsx", $xlWorkbookNormal )
$wb.close
$xl.quit

Errors:

You cannot call a method on a null-valued expression.
At H:\_Algemeen\_Software\_MijnBeheersScript\excel.ps1:6 char:12
+ $cells.Item <<<< (1,1).Value() = 'x' 
    + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At H:\_Algemeen\_Software\_MijnBeheersScript\excel.ps1:10 char:11
+ $wb.SaveAs <<<< ("H:\xlstest.xlsx", $xlWorkbookNormal )
    + CategoryInfo          : InvalidOperation: (SaveAs:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>