Prevent error: "Unable to get the SaveAs property of the Workbook class"

This commit is contained in:
Edward Miller
2024-05-21 15:56:53 -05:00
parent fa907da4a4
commit 66c0fee1e9

View File

@@ -47,12 +47,16 @@ function ConvertTo-ExcelXlsx {
try {
$Excel.Visible = $false
$null = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
if ($null -eq $workbook) {
Write-Host "Failed to open workbook"
} else {
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
}
}
finally {
if ($null -ne $Excel.ActiveWorkbook) {
$Excel.ActiveWorkbook.Close()
if ($null -ne $workbook) {
$workbook.Close()
}
$Excel.Quit()