编程语言
首页 > 编程语言> > C#运行带参数的excel宏

C#运行带参数的excel宏

作者:互联网

我试图以编程方式打开一个Excel工作簿并运行一个宏,该宏接受在命令行中键入的参数.到目前为止,我能够打开工作簿并执行宏,但是我无法传入参数.

我的代码目前:

 public void runTemplate(string templateName, string sourceFile, string destinationFile, string ITPath, string date)
    {
        string template = templateName + "!DoTheImport";
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        ExcelApp.DisplayAlerts = false;
        object misValue = System.Reflection.Missing.Value;
        ExcelApp.Visible = false;
        Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);
        RunMacro(ExcelApp, new Object[] { template });
        ExcelWorkBook.SaveCopyAs(destinationFile);
        ExcelWorkBook.SaveCopyAs(ITPath);
        ExcelWorkBook.Close(false, misValue, misValue);
        ExcelApp.Quit();
        if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
        if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
    }

    private void RunMacro(object oApp, object[] oRunArgs)
    {
        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
    }

我的宏看起来像:

Sub DoTheImport(sDate As String)


With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;\\filePath\DecisionsByRegion-" + sDate + ".txt", 
    Destination:=Range("$A$2") _)
    .Name = "test"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = True
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
Columns("A:G").EntireColumn.AutoFit
Columns("H").EntireColumn.Delete

End Sub

正如我所说,这适用于执行宏(sDate最初是Now()格式化为sub中的字符串,未传递如图所示),但我试图将’date’变量传递给runTemplate并传递它作为sDate进入我的宏.我试过简单地将它添加到参数对象RunMacro(ExcelApp,new Object [] {template,date});但这引发了一个错误.

解决方法:

虽然我无法使用现有方法传递多个变量,但我找到了一种执行宏的替代方法,它允许我根据需要传递参数.

public void runTemplate(string templateName, string sourceFile, string destinationFile, string ITPath, string date)
{
    string sDate = date;
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
    ExcelApp.DisplayAlerts = false;
    object misValue = System.Reflection.Missing.Value;
    ExcelApp.Visible = false;
    Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);
    ExcelApp.Run("DoTheImport", sDate);
    ExcelWorkBook.SaveCopyAs(destinationFile);
    ExcelWorkBook.SaveCopyAs(ITPath);
    ExcelWorkBook.Close(false, misValue, misValue);
    ExcelApp.Quit();
    if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
    if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}

我删除了RunMacro方法,只使用了ExcelApp.Run(“DoTheImport”,sDate);执行宏.这个方法允许我将参数传递给宏,可以通过添加’ByVal’参数传递机制在宏中访问它:

Sub DoTheImport(ByVal sDate As String)

标签:c,excel-vba,excel-interop
来源: https://codeday.me/bug/20190701/1346562.html