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 { try {
$Excel.Visible = $false $Excel.Visible = $false
$null = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true) $workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat) if ($null -eq $workbook) {
Write-Host "Failed to open workbook"
} else {
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
}
} }
finally { finally {
if ($null -ne $Excel.ActiveWorkbook) { if ($null -ne $workbook) {
$Excel.ActiveWorkbook.Close() $workbook.Close()
} }
$Excel.Quit() $Excel.Quit()