代码之家  ›  专栏  ›  技术社区  ›  Barry Piccinni

C运行带参数的Excel宏

  •  4
  • Barry Piccinni  · 技术社区  · 8 年前

    我试图以编程方式打开一个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 }); 但这引发了一个错误。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Barry Piccinni    8 年前

    虽然我不能用我现有的方法传递多个变量,但是我找到了一个替代的方法来执行宏,它允许我根据需要传递参数。

    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)

    推荐文章