Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No 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 ? Posted 11 mins ago Kim Seok


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");
        }


    }
}
Posted

1 solution

You should use Automation (COM).
Have a look at Microsoft's article: "Office Automation Using C++" and Naren Neelamegam's one: "MS Office OLE Automation Using C++", here at CodeProject.
:)
 
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