Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one main project with a form and several subprojects with forms. I call them subprojects because I created them separately and then added them to the Main project using add/existing item.

There is a global.dll that contains the static string theWorkBook, theWorkbook (the excel file) is then assigned to the main parent project using
RegGlobals.theWorkbook = RegGlobals. objApp.Workbooks.Open(RegGlobals.StrPath);


strPath is the complete path and file name.

When a child project/form opens, I get another instance of the excel file.

I hope this is clear; if not, please ask me to clarify things.

What I have tried:

I have made doubly sure that there is an open statement in only one place.
Made sure theWorkBook is only assigned in the main project.
Made sure theWorkBook was only created in the global dll.
Posted
Updated 10-Jul-22 5:00am
v3
Comments
Member 15627495 10-Jul-22 1:28am    
I think it's a n overload with your command to load your workbook,
in the main form the problem isn't seen. ( because of a good embeding status in form ),
but when loading the others, the workbook go through by excel and open a second "session".

as there is not more code : both things
- clear properly ( out of memory ) the first workbook before loading another one
or
- use several ( an array of workbook ) storage to load your workbooks in all forms


this is a Hard question with so little code to read.
PaulaJoannAllen 10-Jul-22 6:41am    
In the main form:
using RegGlobals = FoodPantryGlobals.GlobalConst;
using NewMonth;
using NameSearch;
using GetDataSheets;

The calling routine in the main form:
private void cmdOpenDataSheets_Click(object sender, EventArgs e)
{
this.Hide();
frmGetDataSheet pantryForm = new frmGetDataSheet();
pantryForm.Show();
}

In the other forms:
using RegGlobals = FoodPantryGlobals.GlobalConst;
using Food_Pantry;


I have moved the declaration and assignment of theWorkBook out of the FoodPantryGlobals and into the main program frmFoodPantry. With the same results.

I am not sure what other code to include. Please tell me if you need more.



We can't tell: we have no access to your code, and particularly not while it is running.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the line that opens the file to monitor how often it is actually being called, and run your code through the debugger. You can then use the stack trace to find out where it is being called from each time. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Found the problem

 //       
public static Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();


Was in every form; I moved it to the main form and deleted it from all other forms.

Thank you for your help
 
Share this answer
 

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