简体   繁体   English

如何通过C#代码从文本文件中将vba宏加载到excel文件?

[英]How to load vba macros to the excel file from a text file by a C# code?

My C# code has to create an Excel file with two Worksheets and output some data over there. 我的C#代码必须创建一个包含两个工作表的Excel文件,并在那里输出一些数据。 Besides data columns, the Sheet 1 has to be enabled with a VBA macros which would allow a user to perform some mathematical calculations with provided data upon clicking on a particular cell. 除了数据列之外,还必须使用VBA宏启用Sheet 1,这将允许用户在单击特定单元格时使用提供的数据执行某些数学计算。 This VBA macros are stored in a text file, like C:\\VBA_MACROS\\VBA1.txt. 此VBA宏存储在文本文件中,如C:\\VBA_MACROS\\VBA1.txt.

Right now I can do it manually, ie 现在我可以手动完成,即

  1. C# code creates an Excel file and populates it with data. C#代码创建一个Excel文件并用数据填充它。
  2. I do a right click on Sheet1 and select an option "View Code". 我右键单击Sheet1并选择“查看代码”选项。
  3. I click on the button "Insert" from the Microsoft Visual Basic for Applications Menu and load the file C:\\VBA_MACROS\\VBA1.txt . 我单击Microsoft Visual Basic for Applications菜单中的“插入”按钮并加载文件C:\\VBA_MACROS\\VBA1.txt
  4. I close the VBA code window. 我关闭了VBA代码窗口。

Question: can steps 2 - 4 be performed automatically by a C# code as well as the step 1? 问题:步骤2 - 4可以通过C#代码以及步骤1自动执行吗? In this case a user would not have to perform them manually which would be a way more comfortable for her. 在这种情况下,用户不必手动执行它们,这对她来说更舒适。

To be exact, this is how the application is created: 确切地说,这就是应用程序的创建方式:

Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Sheets[1];
Excel.Worksheet worksheet2 = workbook.Sheets[2];

// output data to the worksheets
DataTable2Worksheet(tableMain, worksheet, verSize);
DataTable2Worksheet(tableExtra, worksheet2, 0);

// output workbook to the file
string fileDir = @"D:\MyTests\ExcelTests\";
Output2File(fileDir, workbook);

DataTable2Worksheet and Output2File functions are quite trivial, but how to attach the content of the text file to worksheet = workbook.Sheets[1] by using AddFromFile method? DataTable2WorksheetOutput2File函数非常简单,但是如何使用AddFromFile方法将文本文件的内容附加到Output2File worksheet = workbook.Sheets[1]

You'll need a reference to Microsoft.Vbe.Interop; 你需要一个对Microsoft.Vbe.Interop;的引用Microsoft.Vbe.Interop; .

Then you need to get a handle on the module you want to insert into. 然后,您需要获得要插入的模块的句柄。 Then you can just use the CodeModule.AddFromFile method to insert the code in your text file into the module. 然后,您可以使用CodeModule.AddFromFile方法将文本文件中的代码插入到模块中。

VBE.VBProjects("NameOfProject").VBComponents.Item("NameOfWorksheet").CodeModule.AddFromFile("C:\Path\to\file.txt");

The default name for a newly created project is "VBAProject" and you the name of the component for a sheet is the name of the sheet. 新创建的项目的默认名称是"VBAProject"并且工作表的组件名称是工作表的名称。

So, for your particular case, you could add this line of code at the end of your snippet to insert the VBA into Sheet1. 因此,对于您的特定情况,您可以在代码段的末尾添加此行代码以将VBA插入到Sheet1中。

application.VBE.VBProjects("VBAProject").VBComponents.Item("Sheet1").CodeModule.AddFromFile("C:\VBA_MACROS\VBA1.txt");

I just learned that another option is to use the VBProject property of the Workbook , which makes the call a little cleaner. 我刚学会了另一种选择是使用WorkbookVBProject属性 ,这使得调用更加清晰。

workbook.VBProject.VBComponents.Item("Sheet1").CodeModule.AddFromFile("C:\VBA_MACROS\VBA1.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM