从C#运行宏?

时间:2016-08-17 13:17:12

标签: c# wpf vba macros

好的,我知道您可以使用Microsoft.Office.Interop.Excel;在c#程序中使用VBA命令。

我面临的问题是我有一个VBA,它有近10,000行代码,并将其转换为C#兼容命令是不切实际的。它创建一个工作簿并执行数据操作和格式化,以便在用户请求时打印。

有没有办法在C#中存储宏并创建一个我可以按原样运行宏的工作簿?

3 个答案:

答案 0 :(得分:2)

您可以在Excel中运行宏。 看看这个:

Running an Excel Macro via C#: Run a macro from one workbook on another?

http://oakdome.com/programming/CSharp_RunExcelMacro.php

如果您不希望Excel在执行期间可见,请将Excel.ApplicationClass的Visible属性设置为false。

答案 1 :(得分:0)

以下是此解决方案的示例。我创建了工作簿HelloWorldVBACodeExample.xlsm。然后在此工作簿中的Module1和VBA代码中:

Sub HelloWorld(word)

  MsgBox "Hello world and " & word

End Sub

然后使用C#代码创建控制台应用程序:

using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
using System;
using System.IO;

namespace ConsoleAppCallVBA
{
    class Program
    {
    static void Main(string[] args)
    {

        string errorMessage = string.

        #region Check is Excel is installed in the PC on which program is executed

        _Application excel = new _Excel.Application();
        if (excel == null)
        {

            errorMessage = "EXCEL could not be started." + "\n" +
                "This program is able to form reports only on PC with installed Excel. " + "\n" +
                "Check that your office installation is correct.";
            Console.WriteLine(errorMessage);
        }

        #endregion

        excel.Visible = true;

        string fileName = "HelloWorldVBACodeExample.xlsm";
        string pathToExcelXlsmFile = Path.Combine(path, fileName);

        Workbook wb;
        Worksheet ws;
        int sheetNumber = 1;
        wb = excel.Workbooks.Open(pathToExcelXlsmFile);
        ws = wb.Worksheets[sheetNumber];

        //Call VBA code
        excel.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, excel, new Object[] { "HelloWorldVBACodeExample.xlsm!HelloWorld", "My Name"});


        #region Close excel object - release memory from this application

        excel.Quit();
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel);

        #endregion
        }
     }
 }

在执行期间,此C#代码执行了VBA代码,并在消息框文本“ Helo world and My Name”中显示。在使用此代码段的情况下,必须为字符串变量路径分配正确的目录路径,该目录将是带有VBA代码的xlsm文件。

答案 2 :(得分:0)

您将必须将msoAutomationSecurity设置为低,然后将宏作为字符串执行。但是,使用完电子表格后,您可能希望将安全性设置重置为高。

这里是一个例子:

    public void ExecuteExcelMacro(string sourceFile)
    {
        ExcelApp.Application ExcelApp = new ExcelApp.Application();
        ExcelApp.DisplayAlerts = false;
        ExcelApp.Visible = false;
        ExcelApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
        ExcelApp.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);

        string macro = "Sheet1.SendEmailToUser";

        try
        {
            ExcelApp.Run(macro);
            Console.WriteLine("Macro: " + macro + " exceuted successfully");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to Run Macro: " + macro + " Exception: " + ex.Message);
        }

        ExcelWorkBook.Close(false);
        ExcelApp.Quit();
        if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
        if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
    }