Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to run MFC or C++ code behind with a click of a button on a Worksheet, without VBA code.

Can you give me a tip if it is possible ? :rolleyes: :rolleyes:
Posted

1 solution

This is a C# code behind with a click of a button on a Worksheet, without VBA code.

I want to run MFC or C++ code like this.

Plz, give me a tip to me :) :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using MSForms = Microsoft.Vbe.Interop.Forms;
namespace ExcelTest
{
    public partial class Form1 : Form
    {
        public Excel.Application theApp = null;
        public Excel._Workbook theWorkbook;
        public Excel._Worksheet theWorkSheet;
        public Excel.Range theRange;
        Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
        private MSForms.CommandButton btnExcel;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            theApp = new Excel.Application();
            theApp.Visible = true;
            EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
            theApp.WorkbookBeforeClose += EventDel_BeforeBookClose;
            theWorkbook = theApp.Workbooks.Open(@"C:\test.xlsx", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            theWorkSheet = (Excel.Worksheet)theWorkbook.Worksheets.get_Item(1);
            btnExcel = (MSForms.CommandButton)FindControl("btnExcel");
            btnExcel.Click += new MSForms.CommandButtonEvents_ClickEventHandler(btnExcel_Click);
        }
        private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
        {
            MessageBox.Show("Excel Close");
        }
        object FindControl(string name)
        {
            return FindControl(name, (Excel.Worksheet)theWorkbook.ActiveSheet);
        }
        object FindControl(string name, Excel.Worksheet sheet)
        {
            Excel.OLEObject theObject;
            try
            {
                theObject = (Excel.OLEObject)sheet.OLEObjects(name);
                return theObject.Object;
            }
            catch
            {
                // Returns null if the control is not found.
            }
            return null;
        }
        private void btnExcel_Click()
        {
            MessageBox.Show("Button Clicked");
        }

    }
}
 
Share this answer
 
Comments
Member 11269856 10-Dec-14 19:08pm    
Hi! Thanks for this thing!!! But I'm not professional and can't build this solution. If You can please share this Project for VS 2010 or 2013. I need it in C#. Thanks!!!

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