gpt4 book ai didi

C# 运行带参数的 excel 宏

转载 作者:行者123 更新时间:2023-11-30 12:20:27 25 4
gpt4 key购买 nike

我正在尝试以编程方式打开一个 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 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768439/

25 4 0
文章推荐: swift - 扫描ISBN码后出错
文章推荐: javascript - 如何多次使用
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com