Question Prevent manually opening Excel instance during Automation

p2kpradeep

New member
Joined
Nov 10, 2010
Messages
1
Programming Experience
3-5
Hi,

I have a VB .Net windows application that generates reports in excel files from a dataset.

I use "New Excel.Application" to create the application, delete all but one worksheet to begin with, and push data from my dataset in a loop. During this process, I do not show the Excel GUI to the user, so nobody can tamper with the instance. The application works without any errors.

Except in one case: When the automation is in progress, if the user opens some other excel file, it opens as a workbook within the Excel instance being used for automation, instead of creating a new Excel application. In such cases, the automation is exposed, and fails with random exceptions.

So when the automation is in progress, I would like to prevent the user from opening the instance of Excel that I use in my VB application.

What is best solution for this?
 
Whether Excel application uses single or multiple instances is a user setting, in Excel 2003 this is in Options (View tab: checkbox Show Windows In Taskbar), the automation object (Application) also has a ShowWindowsInTaskbar property. This may enable you to force single instance, but since it is a user setting the user may not agree :) Anyway, you can also hide the workbook from being visible in an interactive Excel session like this:
VB.NET:
book.Windows(1).Visible = False
'(book variable is the reference to the Workbook instance)
 

Latest posts

Back
Top