Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel Interop
Hello,
 
I created a new Excel application with:
 
Application xlApp = new Application ();
Workbooks wbs = xlsApp.Workbooks;
Workbook wb = wbs.Add (1);
...
 
If the user opens "by hand" an Excel file, it will open in this instance.
 
If an Excel file is opened before the code is executed, then it is in it that will open Excel files.
 
In summary, Excel files open :
 
- in a new instance if there was not before
- in the first instance of Excel open
 
So I would make my Excel xlApp "default" for open Excel file, especially if another instance of Excel is opened before.
 
thank you,
Posted 17-Apr-13 23:00pm
Comments
Kenneth Haugland at 18-Apr-13 4:04am
   
Not entirely sure what you want here. Do you want any xls file that is double clicked to open your applicationg, if so the registary is the way to do this.
Member 9565931 at 18-Apr-13 4:57am
   
I saw that you could force open a new instance by default by modifying the registry, but this is not quite what I want.
I want a certain instance of Excel must to be one in which open Excel files by default.
Kenneth Haugland at 18-Apr-13 5:04am
   
Im afraid I dont understand you here.
Member 9565931 at 18-Apr-13 5:15am
   
What I want is that Excel files open "by hand" open in my own Excel instance I created.
Maciej Los at 18-Apr-13 4:12am
   
Not clear. Do you want to check if Excel was opened before?
Member 9565931 at 18-Apr-13 4:53am
   
Not. I want my xlApp to be the Excel instance in which opens by default all Excel files.
Maciej Los at 18-Apr-13 4:58am
   
Describe your needs more precisely...
Member 9565931 at 18-Apr-13 5:15am
   
What I want is that Excel files open "by hand" open in my own Excel instance I created.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have a look at the solutions and comments to this similar problem (leave a comment to this solution if it doesn't quite fix your problem)
 
Problem with Excel object[^]
  Permalink  
Comments
Member 9565931 at 18-Apr-13 5:58am
   
It doesn't quite fiy my problem. Let me tell you more details :
 
I use the WebBrowser component. This allows me to open an Excel file in my application. It works like a web browser: I Navigate ("C:. ... / File.xlsx"), and I catch the event of the end of loading, and I recovered the instance of excel with this code:
 
var obj = (WebBrowser)_webBrowser.ActiveXInstance;
object doc = obj.Document;
XlWorkBook = (Workbook)doc;
XlApplication = XlWorkBook.Application;
 
However, I have no control over the behavior of the component. If Excel is already open, it will open the workbook in this instance. But I wish it opens every time in a NEW instance.
CHill60 at 18-Apr-13 6:25am
   
I'm getting confused - in your post you are using interop to open Excel but now you say you are using a WebBrowser to display the spreadsheet. But it's still the same instance of Excel that is being used (unless you force it to be another one). I think what you're really after is that any spreadsheets opened are displayed in your webbrowser
CHill60 at 18-Apr-13 7:04am
   
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you want to check if MS Excel has been previously opened, see this: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getactiveobject.aspx[^]. If not, please, be more specific and post a comment, and i promise to improve my answer.
  Permalink  
Comments
Member 9565931 at 18-Apr-13 5:19am
   
This is not really what I want to do.
What I want is that Excel files opened "by hand" open by default in my own Excel instance I created.
Maciej Los at 18-Apr-13 5:41am
   
I'm a bit confused ;( Excel is a single-instance application. By default it opens files in the same instance. Please, describe what do you want to achieve, why do you want to "catch" files opened "by hand"?
Member 9565931 at 18-Apr-13 5:54am
   
The "by hand" don't really matter.
I use the WebBrowser component. This allows me to open an Excel file in my application. It works like a web browser: I Navigate ("C:. ... / File.xlsx"), and I catch the event of the end of loading, and I recovered the instance of excel with this code:
 
var obj = (WebBrowser)_webBrowser.ActiveXInstance;
object doc = obj.Document;
XlWorkBook = (Workbook)doc;
XlApplication = XlWorkBook.Application;
 

However, I have no control over the behavior of the component. If Excel is already open, it will open the workbook in this instance. But I wish it opens every time in a NEW instance.
Maciej Los at 18-Apr-13 6:04am
   
So, my answer is good for you, because, you can check if Excel has been already opened then to provide many actions, for example: close this instance of application. I suggest you to display message to the user before you close active instance of Excel.
Tip: saved files has specified Path property, new files (not saved yet), hasn't.
Member 9565931 at 18-Apr-13 6:08am
   
I had envisaged to do this, but I would have preferred a solution that does not cause the close of Excel files.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can change values of the default program for .xls in the Windows registry.
 
A link to the Registry class
  Permalink  
Comments
Member 9565931 at 18-Apr-13 5:20am
   
I saw that you could force open a new instance by default by modifying the registry, but this is not quite what I want. I want a certain instance of Excel must to be one in which open Excel files by default.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100