Worksheets.Item / Worksheet.Cells properties working in unexpected way

0

Edit: Ignore - this is actually behaving this way because of some code outside the scope of this script. My mistake. I just submitted a request to close this post.

I have a simple PowerShell script that is supposed to delete the first line from an Excel .xls file.

My Script is as follows:

$file = $args[0]


    # Start Excel, hide window
    $excel = new-object -com Excel.Application -Property @{Visible = $false} 
    $workbook = $excel.Workbooks.Open($file) # Open the file
    $workbook.CheckCompatibility = $False
    $sheet = $workbook.Sheets.Item("Acutal Worksheet") # Select appropriate worksheet
    [void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row

    $workbook.Close($true) # Close workbook and save changes
    $excel.quit() # Quit Excel
    [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

When I run this, the PoSh script finishes clean, but when I open the Workbook, I notice that the first two rows have been deleted. I only want the first row to be deleted.

Now, when I run the script with a worksheet name that does not exist in the workbook, the script throws the following error and when I open the Workbook, only the first row has been deleted from the target worksheet. Note: There is only a single worksheet within the workbook. It is producing my desired outcome, but this is ugly and I need to implement something that makes sense to me. What needs to be changed in order for it to work when I specify the correct Worksheet name?

Example of script with name that does not exist in workbook:

$file = $args[0]


    # Start Excel, hide window
    $excel = new-object -com Excel.Application -Property @{Visible = $false} 
    $workbook = $excel.Workbooks.Open($file) # Open the file
    $workbook.CheckCompatibility = $False
    $sheet = $workbook.Sheets.Item("WRONG_NAME") # Select appropriate worksheet
    [void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row

    $workbook.Close($true) # Close workbook and save changes
    $excel.quit() # Quit Excel
    [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

Error I get from running this:

Exception getting "Item": "Invalid index. (Exception from HRESULT: 0x8002000B (
DISP_E_BADINDEX))"
At I:\path\ps_myScript.ps1:10 char:35
+     $sheet = $workbook.Sheets.Item <<<< ("WRONG_NAME") # Select appropriate works
heet
    + CategoryInfo          : NotSpecified: (:) [], GetValueInvocationExceptio
   n
    + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertyGetValu
   eTI

You cannot call a method on a null-valued expression.
At I:\path\ps_myScript.ps1:11 char:28
+     [void]$sheet.Cells.Item <<<< (1, 1).EntireRow.Delete() # Delete the first
 row
    + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : InvokeMethodOnNull
excel
powershell
etl
asked on Stack Overflow May 2, 2014 by sion_corn • edited May 5, 2014 by sion_corn

1 Answer

1

Since you only have 1 sheet skip the whole $Sheet= line and just change the next line to:

[void]$workbook.ActiveSheet.Cells.Item(1, 1).EntireRow.Delete()# Delete the first row

I use something very similar in a script of mine. I think I use $null= instead of [void], and Item(2, 1) to do the second row instead of the first, but it should work the same.

answered on Stack Overflow May 2, 2014 by TheMadTechnician

User contributions licensed under CC BY-SA 3.0