如何通过 Interop 将函数调用作为参数传递给 Excel 宏?
How to pass a function call as an argument to an Excel macro via Interop?
我正在尝试调用一个 Excel 宏,该宏作为其第一个参数调用一个函数。
假设我在 Excel
中有这个子例程
Sub ValidateSheet(version As Integer, displayNoErrorsBox As Boolean)
我可以像这样在 Excel 中调用:
Call Validation.ValidateSheet(Data.GetVersion, False)
Data.GetVersion
returns 当然是整数。
如何通过 C# Excel 互操作执行此操作?
传递的每个参数都是一个对象。我可以找到所有的教程和帖子传递字符串。是否可以将函数调用作为字符串传递?
这行不通:
m_Application.Run("Validation.ValidateSheet", "Data.GetVersion", "False");
试试这个。
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//~~> Define your Excel Objects
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
//~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open("E:\Users\Siddharth Rout\Desktop\book1.xlsm");
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("ShowMsg", "Hello from C# Client", "Demo to run Excel macros from C#");
//~~> Clean-up: Close the workbook
xlWorkBook.Close(false);
//~~> Quit the Excel Application
xlApp.Quit();
//~~> Clean Up
releaseObject(xlApp);
releaseObject(xlWorkBook);
}
//~~> Release the objects
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
}
}
刚刚使用了评论中的建议并显式调用了第一个宏。
所以,与其尝试这样:
m_Application.Run("Validation.ValidateSheet", "Data.GetVersion", false);
我正在这样做:
string version = m_Application.Run("Data.GetVersion");
m_Application.Run("Validation.ValidateSheet", version, false);
我正在尝试调用一个 Excel 宏,该宏作为其第一个参数调用一个函数。
假设我在 Excel
中有这个子例程Sub ValidateSheet(version As Integer, displayNoErrorsBox As Boolean)
我可以像这样在 Excel 中调用:
Call Validation.ValidateSheet(Data.GetVersion, False)
Data.GetVersion
returns 当然是整数。
如何通过 C# Excel 互操作执行此操作?
传递的每个参数都是一个对象。我可以找到所有的教程和帖子传递字符串。是否可以将函数调用作为字符串传递?
这行不通:
m_Application.Run("Validation.ValidateSheet", "Data.GetVersion", "False");
试试这个。
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//~~> Define your Excel Objects
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
//~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open("E:\Users\Siddharth Rout\Desktop\book1.xlsm");
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("ShowMsg", "Hello from C# Client", "Demo to run Excel macros from C#");
//~~> Clean-up: Close the workbook
xlWorkBook.Close(false);
//~~> Quit the Excel Application
xlApp.Quit();
//~~> Clean Up
releaseObject(xlApp);
releaseObject(xlWorkBook);
}
//~~> Release the objects
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
}
}
刚刚使用了评论中的建议并显式调用了第一个宏。
所以,与其尝试这样:
m_Application.Run("Validation.ValidateSheet", "Data.GetVersion", false);
我正在这样做:
string version = m_Application.Run("Data.GetVersion");
m_Application.Run("Validation.ValidateSheet", version, false);