在工作簿之间翻转并写入数据
Flip Between WorkBooks and write data
我有一个 VSTO 代码调用加载到 VSTO 功能区 Header 的 VBA 代码。我想知道您是否可以(以及如何)将 VBA 代码中的值写入 VSTO 工作簿。我在下面附上了我的代码。
using Microsoft.Office.Tools.Ribbon;
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace Isitpossible
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void callVBA_Click(object sender, RibbonControlEventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
String FilenamePath;
FilenamePath = "C:\Test\MyWorkbook.xlsm"; //Where the workbooks where all the marco are stored
//~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open(FilenamePath,0, true);
xlApp.Visible = false;
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("Test");
}
}
}
我的VBA子写在这里:
Sub Test()
ActiveWorkbook.ActiveSheet.Range("C1").Value = "This Works!"
End Sub
首先,无需从 VSTO 加载项创建新的应用程序实例:
Excel.Application xlApp = new Excel.Application();
要访问宿主应用程序的对象模型,请使用 ThisAddIn
class 的 Application
字段。此字段 returns 表示宿主应用程序当前实例的对象。在 MSDN 的 Access the object model of the host application 部分阅读更多相关信息。
因此,您的代码应如下所示:
private void callVBA_Click(object sender, RibbonControlEventArgs e)
{
Excel.Workbook xlWorkBook;
String FilenamePath;
FilenamePath = "C:\Test\MyWorkbook.xlsm"; //Where the workbooks where all the marco are stored
//~~> Start Excel and open the workbook.
xlWorkBook = Application.Workbooks.Open(FilenamePath,0, true);
xlApp.Visible = false;
Application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("Test");
}
有关详细信息,请参阅 Run a Macro from C#。
我有一个 VSTO 代码调用加载到 VSTO 功能区 Header 的 VBA 代码。我想知道您是否可以(以及如何)将 VBA 代码中的值写入 VSTO 工作簿。我在下面附上了我的代码。
using Microsoft.Office.Tools.Ribbon;
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace Isitpossible
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void callVBA_Click(object sender, RibbonControlEventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
String FilenamePath;
FilenamePath = "C:\Test\MyWorkbook.xlsm"; //Where the workbooks where all the marco are stored
//~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open(FilenamePath,0, true);
xlApp.Visible = false;
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("Test");
}
}
}
我的VBA子写在这里:
Sub Test()
ActiveWorkbook.ActiveSheet.Range("C1").Value = "This Works!"
End Sub
首先,无需从 VSTO 加载项创建新的应用程序实例:
Excel.Application xlApp = new Excel.Application();
要访问宿主应用程序的对象模型,请使用 ThisAddIn
class 的 Application
字段。此字段 returns 表示宿主应用程序当前实例的对象。在 MSDN 的 Access the object model of the host application 部分阅读更多相关信息。
因此,您的代码应如下所示:
private void callVBA_Click(object sender, RibbonControlEventArgs e)
{
Excel.Workbook xlWorkBook;
String FilenamePath;
FilenamePath = "C:\Test\MyWorkbook.xlsm"; //Where the workbooks where all the marco are stored
//~~> Start Excel and open the workbook.
xlWorkBook = Application.Workbooks.Open(FilenamePath,0, true);
xlApp.Visible = false;
Application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
//~~> Run the macros by supplying the necessary arguments
xlApp.Run("Test");
}
有关详细信息,请参阅 Run a Macro from C#。