From 66c0fee1e9d5349a8f8be2b3ab02ad93f5d98794 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Tue, 21 May 2024 15:56:53 -0500 Subject: [PATCH] Prevent error: "Unable to get the SaveAs property of the Workbook class" --- Public/ConvertTo-ExcelXlsx.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Public/ConvertTo-ExcelXlsx.ps1 b/Public/ConvertTo-ExcelXlsx.ps1 index 578445e..06094e7 100644 --- a/Public/ConvertTo-ExcelXlsx.ps1 +++ b/Public/ConvertTo-ExcelXlsx.ps1 @@ -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()