Hi,
We are using Excel files in a shared repository.
If a user does not close the common Excel file, it leads to an error with EasyMorph because the resource is "in use by another process"
What is the best practice if I want to save and close the opened Excel file before inserting a new sheet?
Thanks for your help
I used a PowerShell command an it works :
Target file name (the one to be saved and closed)
$targetFileName = "FileName"
Get the active Excel instance
$excelApp = [Runtime.Interopservices.Marshal]::GetActiveObject("Excel.Application")
Check each open workbook to find the specified file
foreach ($workbook in $excelApp.Workbooks) {
if ($workbook.Name -like "$targetFileName") {
# Save the file
$workbook.Save()
# Close the file
$workbook.Close($false) # Closes without asking for confirmation
Write-Output "The file '$targetFileName' has been saved and closed."
}
}
Release COM memory for Excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
Hi
I’d like to add a note: I haven’t verified the code's accuracy, but your solution could lead to a resource leak.
ReleaseComObject might not execute if an error occurs earlier in the code.
To prevent this, you can place the cleanup code within a finally block.
Many thanks ckononenko