0xC0000005 issue when fetching and using Excel of offce from a specific C#

Suh Changho 0 Reputation points
2024-10-14T04:16:13.88+00:00

How are you?

I'm developing with C#.

Create an Excel application of Microsoft.Office.Interop.Excel using Panel in C# Winform and connect it to Panel

C# I have configured a program to control Excel within the program.

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.

I tried both the old version and the update, but in that version, the process ends.

Is there a way to solve that problem?

Below is a simple implementation of the program.

Thank you.

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 ExcelPanelIntegration

{

    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();

            }

        }

    }

}

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,951 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,996 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,966 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.