Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error opening excel in powershell

Tags:

People also ask

Can PowerShell interact with Excel?

The ImportExcel is a PowerShell module that allows you import to or export data directly from Excel spreadsheets without having Microsoft Excel installed on your computer. In this tutorial, you'll learn to work with Import-Excel and Export-Excel.

Why Excel is not opening in Windows 11?

Uncheck the Ignore Dynamic Data Exchange (DDE) Box. One of the causes of Excel won't open Windows 10/11 is that your Excel is set to ignore other applications that use DDE. The function of the DDE is to send a message to Excel once you double-click a file.


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!