0

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
3
  • Is your excel file open when you were trying to insert an item into it ? Commented Mar 3, 2014 at 15:20
  • What is your $excel object 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) = $item will "Switch" the sheets for you Commented Mar 3, 2014 at 16:12
  • I am switching to another worksheet to update a chart title. Commented Mar 3, 2014 at 16:41

2 Answers 2

2

I found out what my problem was:

In the script I am opening a workbook, checking a cell for a date than comparing that date to the current date. Once I get the difference, I know now how many times I must loop through my report and close the workbook. Than I begin a batch script, which collects a bunch of data for the last 15 days, I then get using a (switch). Once I have my data, I open up excel once again, and begin my reporting.

The issue was the time between closing and reopening excel, I was reopening the same workbook far to quickly before it had fully closed. So whilst parsing in my data, the workbook would than become "available" and interrupt my script throwing these errors.

I figured this out by putting a Start-Sleep -sec 20 before opening excel for the second time, and has yet to give me any issues.

Edit: I have gone and fixed my use of objects correctly. Also, I have adjusted my code to only open excel once, instead of the open-close, open-close that I had been doing.

Sign up to request clarification or add additional context in comments.

Comments

1

I would not be using select or activate, instead, create objects for each sheet, and call the cells.item on the individual sheets, not the entire workbook. So first do $Sheet1 = $WorkBook.Worksheets.Item(1) which creates a worksheet object of the first sheet. Then add your items using $sheet1.Cells.item($row,$col) = $item

There are many sources on why not to use select or activate. Here's one : How do i activate a specific workbook and a specific sheet?

The biggest reasons you should not use it is because of performance, and mainly, it is not necessary.

7 Comments

I never knew this, but I will be incorporating this into my script. Although it does not answer my question. Thanks though!
@BigRabbit "What could be the problem here, why is it so inconsistent?" wasn't that your question? The answer is because your using select.. and when you try to insert items your using the excel object, which is very wrong. Excel contains workbooks which have worksheets, which have cells. The problem is your not being specific enough
because I'm using .select isn't why its breaking, go and test it yourself, that part works. Right now when I run through my script I can see all my data manipulation happening visually. I am being specific just because you pointed out that my PowerShell programming etiquette is sloppy doesn't mean that's my answer (I'm still learning).
@BigRabbit It's not etiquette or style, it's just not how you do it. Please look at a simple tutorial, and notice Application objects are not the way to call Cells. It might work sometimes but is clearly breaking: blogs.technet.com/b/heyscriptingguy/archive/2006/09/08/… .. In this example, $a is the application object, $b the workbook object, and $c the worksheet object... If you still don't understand the difference in these objects, you should read the library :msdn.microsoft.com/en-us/library/wss56bz7.aspx
@BigRabbit That is a hack, not a fix
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.