Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

filling excel from c#

In spite of having this .Visible = false code in place in a c# project:

                  oApp = new Excel.Application();
                  oApp.Visible = false;


If I try to open any (ohter) excel workbook while my project is running, it also opens the workbook that I am filling from code.

Is there something I can do to stop this from happening?
Avatar of Calvin Brine
Calvin Brine
Flag of Canada image

The reason this is happening is because excel opens workbooks in the default one available.  In this case, excel is open but not visible, so when you try and open it you end up having it opened by the hidden instance of excel.  To avoid this, create a second instance of the application, and use it to open the other workbooks.

oApp2=new Excel.Application();
oApp2.Workbooks.Open "C:\test.xls";

or something along these lines, since I don't know C# very well.
HTH
Cal
Avatar of g_johnson

ASKER

That's helpful Cbrine, and I guess I wasn't clear on my question.  Sorry about that.  But the problem is when the user opens another workbook manually.  So, my app is running, the user opens Excel for other purposes, and my workbook loads.

Can that be solved?
Avatar of MalicUK
MalicUK

Hi g_johnson,

The problem is how Cal described - Excel uses the most recently opened instance of excel as the default, which will be the one you created in your code. So creating a second instance of excel AFTER you created the one running your app ~should~ solve the problem:

oApp2=new Excel.Application();
oApp2.Visible = true;

However, no addons will be available in this copy of Excel, nor will a writeable personal.xls file.

MUK.
I don't understand what this means:

However, no addons will be available in this copy of Excel, nor will a writeable personal.xls file.

What are "addons" and what do you mean by "writeable personal.xls" file?

So, in summary, is this just a limitation of using Excel from code?
Hi,

I'm not being rude but to be honest if you are unsure as to what they are then missing them won't affect you ;)

And yes, that is an accurate summary.

MUK.
But they might affect my users ...
If you are installing this on multiple PC's then I would say there is a good chance someone is going to be using an addin.

Cal
so do we all agree that this is just something the users need to be aware of?   That there is no good coding solution?
ASKER CERTIFIED SOLUTION
Avatar of Calvin Brine
Calvin Brine
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS- closing the workbook and the app as well.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial