In C# Winform, we created an Excel application of Microsoft.Office.Interop.Excel using Panel to configure a program to control Excel within the C# program by connecting with Panel.
During development, a specific Office version (Office 2019 Pro 1808 version) caused 0xC0000005 error when changing the cell value and confirmed that the Excel process was terminated.
Excel is terminated only when associated with the main program with the SetParent() function, and Excel is not terminated when SetParent() is not used.
There seems to be a problem with SetParent(), but I can’t figure it out. Is there a solution? Below is the video and a simple code. Thank you.
VideoLink
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;
using System.Diagnostics;
using System.Security.Principal;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Excel.Application excelApp;
private Excel.Workbook workbook;
private Excel.Worksheet worksheet;
private Panel excelPanel;
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int SWP_NOZORDER = 0x4;
private const int SWP_NOACTIVATE = 0x10;
public Form1()
{
InitializeComponent();
excelPanel = new Panel();
excelPanel.Dock = DockStyle.Fill;
this.Controls.Add(excelPanel);
InitializeExcel();
}
private void InitializeExcel()
{
try
{
excelApp = new Excel.Application();
workbook = excelApp.Workbooks.Add();
worksheet = workbook.ActiveSheet;
// Set Excel window as a child of our panel
SetParent(new IntPtr(excelApp.Hwnd), excelPanel.Handle);
// Resize Excel window to fit the panel
SetWindowPos(new IntPtr(excelApp.Hwnd), IntPtr.Zero, 0, 0, excelPanel.Width, excelPanel.Height, SWP_NOZORDER | SWP_NOACTIVATE);
// Make Excel visible
excelApp.Visible = true;
// Disable Excel's standard error handling
excelApp.DisplayAlerts = false;
}
catch (COMException comEx)
{
MessageBox.Show($"COM Exception during Excel initialization: {comEx.Message}nError Code: {comEx.ErrorCode:X}");
}
catch (Exception ex)
{
MessageBox.Show($"Error during Excel initialization: {ex.Message}");
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
CleanupExcel();
}
private void CleanupExcel()
{
try
{
if (worksheet != null)
{
Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if (workbook != null)
{
workbook.Close(false);
Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (excelApp != null)
{
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during Excel cleanup: {ex.Message}");
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
서창호 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1