저는 C#에서 간단한 Windows Form을 가지고 있으며 Excel 통합 문서를 열고 다른 통합 문서에서 매크로를 실행하는 데 사용합니다. 코드는 다음과 같습니다.ChangeEventHandler를 기존 엑셀 시트에 추가하십시오.
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();
}
이 코드는 새로 만든 통합 문서 작동 : 작동 코드는 다음과 같다.
내가 노력하고 내가 오류 다음 얻을 내 excelfile.xlsx 연 후
EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(CellsChange);
을 추가 할 때 :
"객체 참조가 비 정적 필드, 메서드 또는 속성 '을 위해 필요하다 my_tool.Form1.EventDel_CellsChange ' "
및
"개체 참조가 아닌 정적 필드, 메소드 또는 속성에 필요한'my_tool.Form1.CellsChang e (Microsoft.Office.Interop.Excel.Range) ' "
무엇이 잘못 되었나요?
감사합니다.