Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I created a new Excel application with:

C++
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
Comments
Kenneth Haugland 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 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 18-Apr-13 5:04am    
Im afraid I dont understand you here.
Member 9565931 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 18-Apr-13 4:12am    
Not clear. Do you want to check if Excel was opened before?

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[^]
 
Share this answer
 
Comments
Member 9565931 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 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 18-Apr-13 7:04am    
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.
 
Share this answer
 
Comments
Member 9565931 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 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 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 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 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.
You can change values of the default program for .xls in the Windows registry.

A link to the Registry class
 
Share this answer
 
Comments
Member 9565931 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900