Be able to block excel while it is open

Hello,

I am working with the ExcelEngine. While I am opening the workbook if I try to open manually, I can`t because the ExcelEngine is blocking the workbook, but If I try to open manually after ExcelEngine opened I can.

Is there any method openning the workbook to block it until I close it?

I have the following code:

 Using excelEngine As Syncfusion.XlsIO.ExcelEngine = New Syncfusion.XlsIO.ExcelEngine()
                        Dim application As IApplication = excelEngine.Excel
                        application.UseFastRecordParsing = False
                        Dim workbook As IWorkbook = application.Workbooks.Open(Path, ExcelParseOptions.DoNotParsePivotTable)

                         Code....

                        workbook.Save()
                        workbook.Close()
                        excelEngine.Dispose()
End Using

Thanks in advance.

Best Regards,

Ander.



3 Replies 1 reply marked as answer

SK Shamini Kiruba Sobers Syncfusion Team February 12, 2021 07:34 AM UTC

Hi Ander, 

Greetings from Syncfusion support. 

Workbook can be opened only after the ExcelEngine initialization. The Using statement of ExcelEngine initialization blocks the workbook and the workbook will be automatically closed when the End Using statement is reached. Otherwise, the workbook will be closed only when workbook.Close() is invoked. Hence we suggest you to use the below statement for ExcelEngine initialization instead of the Using statements to avoid automatic closing of workbook. 

Dim excelEngine As Syncfusion.XlsIO.ExcelEngine = New Syncfusion.XlsIO.ExcelEngine() 

If your query is different, kindly clarify us with more details of the issue or let us know if it helps. 

Regards, 
Shamini 



AC Ander Caloca February 12, 2021 08:59 AM UTC

Hi Shamini,

Maybe I didn´t explain correctly, sorry.

What I want is that while worbook is open by ExcelEngine, nobody will be able to open the workbook manually, as much only "read only".

If I can open manually and make changes and save while workbook is open by ExcelEngine when I am going to save by ExcelEngine I overwrite on top of manually made changes and I don´t want that to happen.

Thanks in advance.

Best Regards,

Ander.


SK Shamini Kiruba Sobers Syncfusion Team February 15, 2021 12:00 PM UTC

Hi Ander, 

Thanks for the clarification. 

The Save() method considers the changes done in the ExcelEngine’s workbook object alone and ignores the manually made changes. If this is what you don’t want to happen, then you can open the file as a stream and save the workbook with a different name using SaveAs() method. When the file is opened as a stream, it is not allowed to be opened/edited manually. But also, you cannot save the changes done in the Excel file with ExcelEngine using the Save() method, which will throw an exception. You must use SaveAs() method to save the changes in a different file name as below. 

Code snippet: 

Using excelEngine As Syncfusion.XlsIO.ExcelEngine = New Syncfusion.XlsIO.ExcelEngine() 
    Dim application As IApplication = excelEngine.Excel 
    Dim fileStream = New FileStream(filePath, FileMode.Open, FileAccess.ReadWrite) 
    Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelParseOptions.DoNotParsePivotTable) 
 
    Code.... 
 
    workbook.SaveAs(newFileName) 
    fileStream.Dispose() 
End Using 

Regards, 
Shamini 


Marked as answer
Loader.
Up arrow icon