I need to open excel file with CorruptLoad
paramterer from powershell-script. But when I try to make it, I get an error Exception calling "Open" with "15" argument(s): "open method workbooks class failed"
. This error occurs only when I call Open
with all 15 arguments. And when I try to open the same excel file with VB.net
program with 15 arguments or with specifying value of named argument CorruptLoad
, there is no problem!
I'm using powershell v 4.0
, Office 2010 with SP2 and .NET Framework 4.5.2
.
Here is my powershell
code:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
try
{
$missing = [System.Type]::Missing
# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing)
# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, 1)
$XlCorruptLoad = "Microsoft.Office.Interop.Excel.XlCorruptLoad" -as [type]
$wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
$missing, $missing, $missing, $missing, $missing,
$missing, $missing, $missing, $missing, $XlCorruptLoad::xlRepairFile)
}
catch
{
Write $Error[0].ToString()
}
# some stuff
if ($excel -ne $null)
{
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
$excel = $null
}
[System.GC]::Collect() | Out-Null
[System.GC]::WaitForPendingFinalizers() | Out-Null
I have not idea why error occurs. I'll be glad to any advices and assumptions!