I am having a weird problem with powershell. I have other scripts similar to the one throwing an error on a line I am using several times throughout the script.
I am using the get-content to grab a CSV file. Then I begin to parse through that data only selecting certain columns and outputting them into excel. Here is where one of my problems occurs...
It is very inconsistent, but at some point in time while parsing through the csv I get this error
Exception setting "Item": "Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))"
On this line, which is used to output the data into excel at a specific column and row.
$excel.Cells.Item <<<< ($rowMonth, $columnMonth) = "$item"
I have noticed though, if I hold the scroll bar whilst parsing through the data, I pass over this error. I also noticed it stopped at a different spot almost every time (give or take a few cells of data), or sometimes I don't get the error at all.
Immediately after I get this error for each failed insert I get this error
Assignment failed because [System.__ComObject] doesn't contain a settable property 'Item()'.
...until I am done error out on the parsing section of the script.
I have tried to Google these errors, and there is very little information on PowerShell with Excel already and I'm having a hard time trying to figure out what the problem is (day 3..)
I have 4 other scripts that run this line of code..
$excel.Cells.Item($rowNumber, $column) = "$item"
and they have never show me this error.
Once my parsing has completed (if it get's there, I cheat while I'm trying to debug and hold the scroll bar) I have this line here which I use to switch to another worksheet in excel, and it will error out every single time here.
$sheet2 = $workBook.WorkSheets.Item($sheetName2).Select()
Also giving me the first error I had posted..Also when I do get the error whilst parsing half though, soon as it errors, excel asks me to set the book to read/write (...again...only sometimes.....).
What could be the problem here, why is it so inconsistent? Could it be memory limits? (I have parsed waaay more data in the CSV than I am in the script and have never had this issue)
Please Help SO!
Here is how I am creating my excel object.
#Create an instance of Excel
$excel = New-Object -comobject Excel.Application
#declaring sheet names
$sheetName = "some sheet name"
#open Excel file
[string]$file = "C:\Users\Desktop\excel-file.xlsx"
$workBook = $excel.Workbooks.open($file)
$workBook.WorkSheets.Item($sheetName).activate()
$excel.Visible = $true
$excel.displayAlerts = $false
Here is where I am outputting into excel:
#Declaring variables
$i = 0
$rowNumber = 2
$rowMonth = 2786
$data1 = Get-Content $file1
foreach($row in $data1){
if($row){
$line = $row.Split(",")
$i++
#reset back to the first column once there is no more data for that row
$column = 1
$columnMonth = 7
$dataColumn = 0
if($i -ge 1){
foreach($item in $line){
$dataColumn++
if($dataColumn -eq 1 -or $dataColumn -eq 2 -or $dataColumn -eq 5 -or $dataColumn -eq 7){
$excel.Cells.Item($rowNumber, $column) = "$item"
$excel.Cells.Item($rowMonth, $columnMonth) = "$item"
$column++
$columnMonth++
}#endif
}#end foreach
$rowNumber++
$rowMonth++
}#end if $i
}#end if $row
}#end foreach
$excelobject and how are you creating it? Also, why do you need to switch to another worksheet? You should just do$sheet2 = $workBook.WorkSheets.Item(2)and$sheet2.Name = $sheet2name... Making calls to$sheet2.cells.item($row,$col) = $itemwill "Switch" the sheets for you