diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index b80790c..14ac83b 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -458,41 +458,46 @@ function Convert-Files { # Convert the file to XLSX $xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx" - # Use Join-Path to properly handle paths with spaces + # Before the Excel conversion, get the full paths and clean them $xlsxPath = Join-Path $env:TEMP $xlsxFileName $downloadPath = Join-Path $env:TEMP $originalFileName - # Convert the paths to proper Windows paths + # Clean the paths and ensure they're valid $xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath) $downloadPath = [System.IO.Path]::GetFullPath($downloadPath) + # Verify the download file exists + if (-not (Test-Path -LiteralPath $downloadPath)) { + throw "Downloaded file not found at: $downloadPath" + } + try { # Open Excel $excel = New-Object -ComObject Excel.Application $excel.Visible = $false - $excel.DisplayAlerts = $false # Suppress Excel alerts + $excel.DisplayAlerts = $false try { - # Convert paths to proper format for Excel - $excelDownloadPath = $downloadPath -replace "\\", "\\" - $excelXlsxPath = $xlsxPath -replace "\\", "\\" - - $workbook = $excel.Workbooks.Open($excelDownloadPath) + # Use proper path handling for Excel + $workbook = $excel.Workbooks.Open([string]$downloadPath) # Save as XLSX using Excel constants $xlOpenXMLWorkbook = 51 # Excel constant for XLSX format - $xlNormal = 1 # Excel constant for normal save - # Use the standard SaveAs method - $workbook.SaveAs($excelXlsxPath, $xlOpenXMLWorkbook) - $workbook.Close($true) # True to save changes + # Create a temporary filename without spaces + $tempXlsxPath = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".xlsx") - Start-Sleep -Seconds 2 # Give Excel more time to finish saving + # Save to temporary file first + $workbook.SaveAs([string]$tempXlsxPath, $xlOpenXMLWorkbook) + $workbook.Close($true) - # Verify the file was created using robust path checking - $verifyPath = [System.IO.Path]::GetFullPath($xlsxPath) - if (-not (Test-Path -LiteralPath $verifyPath)) { - throw "Excel SaveAs succeeded but file not found at: $verifyPath" + # After Excel is closed, copy the file to the desired location + if (Test-Path -LiteralPath $tempXlsxPath) { + Copy-Item -LiteralPath $tempXlsxPath -Destination $xlsxPath -Force + Remove-Item -LiteralPath $tempXlsxPath -Force + } + else { + throw "Excel SaveAs failed to create temporary file" } } finally { @@ -506,7 +511,9 @@ function Convert-Files { [System.GC]::WaitForPendingFinalizers() # Force Excel process cleanup - Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue | Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } | Stop-Process -Force + Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue | + Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } | + Stop-Process -Force } $script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"