Click here to Skip to main content
15,435,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am opening a form called RegisterInput when I call it to open from the main form the excel sheet opens a second time.

Code the fails.
private void cmdRegisterInput_Click(object sender, EventArgs e)
     frmRegisterInput pantryForm = new frmRegisterInput();

Similar code that works.

private void cmdNewMonth_Click(object sender, EventArgs e)
    frmNewMonth pantryForm = new frmNewMonth();

I see no difference between these, and I put brake points in and the line that seemed to open the second excel file is.

frmRegisterInput pantryForm = new frmRegisterInput();

What I have tried:

I put breakpoints in and used F11 to step through the code.
I coped the code that works and used it with the needed modifications
Updated 23-Jul-22 12:02pm
Maciej Los 22-Jul-22 3:22am     CRLF
Somewhere in your code you create new instance of Excel... That's why you have 2 instances of Excel. BTW: You can't open the same workbook twice unless you messed up registry entries... See: How to open same file twice simultaneously in excel 2013[^]
PaulaJoannAllen 22-Jul-22 7:14am    
Besides the open statement, what other statement, would create a new instance? From what I have read and understand, the second instance is read-only.
CHill60 22-Jul-22 7:54am    
This is going to sound silly but ... try restarting your machine. There may be an instance of the program "stuck" in memory (not the correct technical term!). I used to see this a lot with earlier versions of Visual Studio - for me it used to manifest as not being able to build the solution but it's worth a try to at least discount this as an issue
PaulaJoannAllen 23-Jul-22 14:31pm    
Tried that several times with the same results.

So put a breakpoint in the frmRegisterInput constructor and see exactly what it does.

We can't do that for you: we have no access to your code, running or not!
Share this answer
PaulaJoannAllen 21-Jul-22 21:08pm     CRLF
Although I have some programming background, I am new to all this with C#. So sorry if this is real basic. I put a breakpoint on the line in question and on what I think is the constructor. The second excel file is opened after frmRegisterInput pantryForm = new frmRegisterInput(); is executed and before private System.ComponentModel.IContainer components = null; namespace RegisterInput { partial class frmRegisterInput { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// <param name="disposing" />true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) It is not making any sense to me.
I found the problem here,

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

excelApp was already declared.
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