PowerShell code snippet below will get Excel last row on the specified workbook.
It will also find last column used.
Use PowerShell ISE to test, just make sure that on D drive there is a workbook with filename Book1x.xlsx.
Or just change the path and the file name to any values.
To release COM object check out this link below:
Technet site on how to release COM Object
Getting Rid of a COM Object (Once and For All)
http://technet.microsoft.com/en-us/library/ff730962.aspx
Here's the code snippet:
==========================
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Open("D:Book1x.xlsx")
$objWorksheet = $objWorkbook.Worksheets.Item(1)
$objRange = $objWorksheet.UsedRange
$a = $objRange.SpecialCells(11).row
$b = $objRange.SpecialCells(11).column
write-host "Lastrow:", $a, " Last Column:" $b
$objExcel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
==========================
Cheers!!!
----------------------------------
Free Android app, download at Google play:
Android Divine Mercy Chaplet Guide
https://play.google.com/store/apps/details?id=com.dmercyapp
It will also find last column used.
Use PowerShell ISE to test, just make sure that on D drive there is a workbook with filename Book1x.xlsx.
Or just change the path and the file name to any values.
To release COM object check out this link below:
Technet site on how to release COM Object
Getting Rid of a COM Object (Once and For All)
http://technet.microsoft.com/en-us/library/ff730962.aspx
Here's the code snippet:
==========================
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Open("D:Book1x.xlsx")
$objWorksheet = $objWorkbook.Worksheets.Item(1)
$objRange = $objWorksheet.UsedRange
$a = $objRange.SpecialCells(11).row
$b = $objRange.SpecialCells(11).column
write-host "Lastrow:", $a, " Last Column:" $b
$objExcel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
==========================
Cheers!!!
----------------------------------
Free Android app, download at Google play:
Android Divine Mercy Chaplet Guide
https://play.google.com/store/apps/details?id=com.dmercyapp
Thanks for sharing. Just what i needed :-)
ReplyDelete