Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using Microsoft.Office.Interop.Excel to open an excel application within a panel in a form. However, whenever I close Excel in the panel by clicking the "X" button in the excel app, it closes my whole windows forms application as well. Currently using a link label to open excel in the panel, but not sure how to keep the parent form open when excel is closed.

What I have tried:

Not sure what to try as the whole application closes whenever I try to close excel within my program.

Here is my code in C#

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);


var projectspage = System.Windows.Forms.Application.OpenForms.OfType<ProjectsPage>().FirstOrDefault();

excelApp.Workbooks.Open($@"C:\Users\daver\OneDrive\Documents\BMCalcs\{link}.xlsx");
excelApp.Visible = true;
excelApp.ScreenUpdating = true;
excelApp.EnableAutoComplete = false;
IntPtr excelHwnd = new IntPtr(excelApp.Application.Hwnd);
SetParent(excelHwnd, projectspage.pnl_ProjectDoc.Handle);
excelApp.WindowState = XlWindowState.xlMaximized;
Posted
Updated 20-Oct-22 3:28am
v2

1 solution

Hi,

Used the Microsoft example quoted in the comments to make a simple application with three buttons, start excel, disconnect from excel and exit the form.

Did not need to disconnect, closing Excel does not close the application.

Something you are doing with the handle is causing the issue and as I am not sure what you are trying to achieve I cannot say why - rather I know why it is closing the form app just not sure what you are trying to achieve.

namespace WinFormsApp1
{
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using Microsoft.Office.Interop.Excel;
    using System.Runtime.InteropServices;

    public partial class Form1 : Form
    {
        Excel.Application excelApp;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            excelApp = new Excel.Application();
            excelApp.Visible = true;
            excelApp.WindowState = XlWindowState.xlMaximized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            while (Marshal.ReleaseComObject(excelApp) != 0)
                ;

        }
    }
}
 
Share this answer
 
v3
Comments
Member 15777870 20-Oct-22 9:28am    
Here is how I'm using excel interop in C#

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

var projectspage = System.Windows.Forms.Application.OpenForms.OfType<projectspage>().FirstOrDefault();

excelApp.Workbooks.Open($@"C:\Users\daver\OneDrive\Documents\BMCalcs\{link}.xlsx");
excelApp.Visible = true;
excelApp.ScreenUpdating = true;
excelApp.EnableAutoComplete = false;
IntPtr excelHwnd = new IntPtr(excelApp.Application.Hwnd);
SetParent(excelHwnd, projectspage.pnl_ProjectDoc.Handle);
excelApp.WindowState = XlWindowState.xlMaximized;
Michael_Davies 20-Oct-22 9:38am    
So add the :

While System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp) <> 0
Application.DoEvents()
End While

Also see this page for MS advice on Excel & COM

https://learn.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/automate-excel-from-visual-c
Member 15777870 20-Oct-22 10:14am    
Thanks, what is the proper way to do this in C#?
Michael_Davies 20-Oct-22 10:17am    
I used an online VB to C# converter...

while (Marshal.ReleaseComObject(excelApp) != 0)
Application.DoEvents();
Member 15777870 20-Oct-22 10:20am    
Just tried that...it's still closing my whole windows forms application when exiting out of my excel app in my panel.

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