Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ChangeEventHandler添加到现有excel工作表_C#_Excel_Visual Studio 2012_Vba - Fatal编程技术网

C# 将ChangeEventHandler添加到现有excel工作表

C# 将ChangeEventHandler添加到现有excel工作表,c#,excel,visual-studio-2012,vba,C#,Excel,Visual Studio 2012,Vba,我用C#编写了一个简单的Windows窗体,我用它打开一个excel工作簿,并从另一个工作簿中运行宏。代码是: namespace my_tool { public partial class Form1 : Form { //Excel event delegate variables: public Excel.DocEvents_ChangeEventHandler EventDel_CellsChange; public Excel.Work

我用C#编写了一个简单的Windows窗体,我用它打开一个excel工作簿,并从另一个工作簿中运行宏。代码是:

namespace my_tool
{

     public partial class Form1 : Form
     {
    //Excel event delegate variables:
    public Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
    public Excel.Worksheet xlSheet;
    public Excel.Application xlApp;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public static void RunVBATest()
    {
        Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
        oExcel.Visible = true;
        Excel.Workbooks oBooks = oExcel.Workbooks;
        Excel.Workbook oBook1 = null;
        Excel.Workbook oBook = null;
        oBook1 = oBooks.Open("C:\\Reports\\Macros\\excel-sql\\Excel_macro.xlsm");
        oBook = oBooks.Open("C:\\excelfile.xlsx");
        Excel.Worksheet sheet = (Excel.Worksheet)oBook.Sheets["Sheet1"];
        sheet.Select(Type.Missing);
        ((Microsoft.Office.Interop.Excel._Worksheet)sheet).Activate();

        // Run the macro.
        oExcel.Run("'Excel_macro.xlsm'!SQL_bai");

        // Quit Excel and clean up.
        //oBook.Saved = true;
        //oBook.Close(false);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
        //oBook = null;
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
        //oBooks = null;
        //oExcel.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
        //oExcel = null;
        //System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
    }

         private void button1_Click(object sender, EventArgs e)
    {
        RunVBATest();
    }

}
}
我想知道宏何时完成。通过搜索,我发现一种方法是让宏写入一个单元格,并让应用程序检查该单元格中的特定值

要做到这一点,我需要添加一个事件处理程序,但我似乎无法使它与我的文件一起工作。我需要单元格A1上excelfile.xlsx选项卡中Sheet1上的事件处理程序

我发现了一个有效的示例,但当我尝试为我的文件修改它时,它不起作用。 有效的代码如下所示:

//Excel Automation variables:
                 //Excel.Application xlApp;
                 Excel.Workbook xlBook;
                 Excel.Worksheet xlSheet1, xlSheet2, xlSheet3;
    //
                 //Excel event delegate variables:
                 Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
                 Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
    //
                 private void StartExcelAndSinkEvents()
                 {
                     //Start Excel, and then create a new workbook.
                     xlApp = new Excel.Application();
                     xlBook = xlApp.Workbooks.Add(Missing.Value);
                     xlBook.Windows.get_Item(1).Caption = "XL Event Test";
                     xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
                     xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2);
                     xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3);
                     ((Microsoft.Office.Interop.Excel._Worksheet)xlSheet1).Activate();

                     //Add an event handler for the WorkbookBeforeClose Event of the
                     //Application object.
                     EventDel_BeforeBookClose =
                        new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
                     xlApp.WorkbookBeforeClose += EventDel_BeforeBookClose;

                     //Add an event handler for the Change event of both worksheet objects.
                     EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(CellsChange);

                     xlSheet1.Change += EventDel_CellsChange;
                     xlSheet2.Change += EventDel_CellsChange;
                     xlSheet3.Change += EventDel_CellsChange;

                     //Make Excel visible and give the user control.
                     xlApp.Visible = true;
                     xlApp.UserControl = true;
                 }

                 private void CellsChange(Excel.Range Target)
                 {
                     //This is called when any cell on a worksheet is changed.
                     Debug.WriteLine("Delegate: You Changed Cells " +
                        Target.get_Address(Missing.Value, Missing.Value,
                        Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value) +
                        " on " + Target.Worksheet.Name);
                     MessageBox.Show(Target.get_Address(Missing.Value, Missing.Value,
                        Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value));
                 }

                 private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
                 {
                     //This is called when you choose to close the workbook in Excel.
                     //The event handlers are removed, and then the workbook is closed 
                     //without saving the changes.
                     Wb.Saved = true;
                     Debug.WriteLine("Delegate: Closing the workbook and removing event handlers.");
                     xlSheet1.Change -= EventDel_CellsChange;
                     xlSheet2.Change -= EventDel_CellsChange;
                     xlSheet3.Change -= EventDel_CellsChange;
                     xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose;
                 }

         private void button2_Click(object sender, EventArgs e)
    {
        //StartExcelAndSinkEvents();
    }
此代码适用于新创建的工作簿

当我尝试添加

EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(CellsChange);
打开excelfile.xlsx后,出现以下错误:

“非静态字段、方法或属性'my_tool.Form1.EventDel_CellsChange'需要对象引用”

非静态字段、方法或属性“my_tool.Form1.CellsChange(Microsoft.Office.Interop.Excel.Range)”需要对象引用

我做错了什么


谢谢。

我设法让它工作了。我从一个静态方法调用了一个非静态属性

我已经将“publicstaticvoidrunvbatest()”替换为“publicsvoidrunvbatest()”,它现在可以工作了

我真蠢。也许我的回答会帮助其他处于同样情况的人

多谢各位。 达努特