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
{
}
return null;
}
private void btnExcel_Click()
{
MessageBox.Show("Button Clicked");
}
}
}