Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "lock" excel while interacting via COM

Tags:

c#

excel

com

I have an .NET application that reads a database, does some analysis, and updates stats in an Excel spreadsheet using the COM interface. I have the application to the point where it opens the workbook (or detects it, if it's already open), and finds the sheet it needs to update.

I'm encountering an issue where the user can still interact with Excel (close the application/workbook, change data, etc.) while my application is running. I've considered hiding Excel while my app is chewing on data, but that is application-wide and prevents the user from interacting with any open spreadsheet.

Is there a way to lock Excel from changes through the COM interface, but still have it viewable/readable by the user? Alternatively, is there a way to just hide/lock a single workbook?

like image 725
gregsdennis Avatar asked Oct 05 '22 13:10

gregsdennis


2 Answers

Application.Interactive=false;
like image 82
deVashe Avatar answered Oct 10 '22 03:10

deVashe


    Application.Interactive=false;

as Sid suggests is your best bet I would suggest also changing:

    Application.ScreenUpdating = false; // to avoid screen flicker
    Application.DisplayAlerts = false; // if you wish to suppress most excel messages
    Application.EnableEvents = false; // if there is vba in the workbook you wish to avoid triggering

    Application.Calulation = xlCalculationManual; // if it's a calc intensive automation

A good idea to collect your status pre your automation and set all of these properties back to their originals when you are finished with your automation.

like image 26
Scott Gall Avatar answered Oct 10 '22 03:10

Scott Gall