有没有办法将今天的日期作为命令行参数从 Windows 任务计划程序作业传递给 Excel

问题描述 投票:0回答:1

我们有一个 Windows 任务计划程序作业,它运行一个 Excel xlsm 文件,其中包含一些命令行参数,如下所示:

    Program/script:
    "C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"

    Add arguments (optional):
    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2

我们想添加第三个参数与今天的日期,它将显示在任务管理器中,如下所示:

    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2,03-30-2024

该要求的原因是该作业有时会出现问题,而我们有一个需求作业,该作业通过 WMI 进程旋转并根据检查命令行终止正在运行的 Excel 实例。在任务管理器命令行列中查看日期确实很有帮助。

当您在 google 上搜索 Excel 命令行参数时,会发现有很多针对各种事物的示例,但我找不到涉及日期命令行参数的示例。我试过了:

    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2,%date
    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2,%date%
    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2,%Now
    "C:\File\Path\MyMacroEnabledFile.xlsm" /e/Arg1,Arg2,%Now%
excel vba scheduled-tasks command-line-arguments
1个回答
0
投票

你没有回答我的澄清问题...

  1. 工作簿中没有任何命令行可以写入。您可以在此处查看可用命令行的列表。

您可以打开 Excel 应用程序、工作簿或更多内容,有或没有启动屏幕、只读等。

  1. 为了使用要发送到 Excel 工作簿的参数/参数,您需要一种方法来 读取命令行并处理它以提取参数。知道这一点后,传递任何字符串作为参数就足够了,比如“日期”,并且当它作为最后一个存在时,指示 VBA 代码放置当前日期。
    
    
  2. 没有显示能够捕获命令行并查看它对提取的参数有何作用的代码,很难(避免不可能)获得帮助......

假设您了解一点 Excel VBA 编码,我将放置一段代码,能够处理您命名的参数,并对任何此类参数执行某些操作,以逗号分隔:
  1. 3.1 插入一个标准模块,命名为“CmdLineExtractString”,然后复制到下面的代码中:

argument

我仅在64位环境下测试。我没有可用的 32 位计算机。我在互联网上找到了代码
仅适用于32位

,并适应了64位...当我适应时,我认为它也应该适用于32位。但我没有测试过。 3.2 在

Option Explicit #If VBA7 Then Declare PtrSafe Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As LongLong Declare PtrSafe Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) #Else Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long Declare Function lstrlen Lib "kernel32" (ByVal lpString As string) As Long Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (MyDest As Any, MySource As Any, ByVal MySize As Long) #End If Public Function CmdLineToStr() As String 'Returns the whole command line used to open Excel: Dim Buffer() As Byte, StrLen As Long #If VBA7 Then Dim CmdPtr As LongLong #Else Dim CmdPtr As Long #End If CmdPtr = GetCommandLine() If CmdPtr > 0 Then #If VBA7 Then StrLen = lstrlen(CmdPtr) * 32 #Else StrLen = lstrlen(CmdPtr) * 2 #End If If StrLen > 0 Then ReDim Buffer(0 To (StrLen - 1)) As Byte CopyMemory Buffer(0), ByVal CmdPtr, StrLen CmdLineToStr = Buffer End If End If End Function Function ExtractArguments(strCmd As String) As String ExtractArguments = Mid(strCmd, InStr(strCmd, "/")) End Function Sub ProcessArguments(strArgs As String) Dim arr, ws As Worksheet, lastEmptyR As Long arr = Split(Trim(Mid(strArgs, InStrRev(strArgs, "/") + 1)), ",") If InStr(arr(UBound(arr)), "Date") > 0 Then arr(UBound(arr)) = Now 'place the current date/time instead of "Date" string Set ws = ThisWorkbook.Worksheets("Sheet1") lastEmptyR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row + 1 'determine the last empty cell in A:A ws.Range("A" & lastEmptyR).Resize(, UBound(arr) + 1).Value = arr 'drop the array content in A:C, on lastEmptyR End Sub

代码模块中插入下一个代码(自动打开事件):

ThisWorbook

它将返回“Sheet1”工作表的最后一个空行(在 A:A 中)的 A:C 列中的每个参数。在C:C中会返回实际时间(日期+时间:
Option Explicit Private Sub Workbook_Open() Dim strCmd As String strCmd = CmdLineExtractString.CmdLineToStr 'extract the whole command line used to open the workbook ProcessArguments ExtractArguments(strCmd) 'call the Sub processing arguments... End Sub

),如果只需要当前日期,请将

31.03.2024 22:10:05
替换为
Now
...
现在使用 

Date

窗口进行测试。请按

Run
+
Windows key
,使用下一个命令行:
R

然后按 
excel.exe "C:\File\Path\MyMacroEnabledFile.xlsm" /e/arg1,arg2,Date

...

当然,你需要使用真实的工作簿全名...

我尝试注释无法理解的行。如果仍有不清楚的地方,请随时要求澄清。

© www.soinside.com 2019 - 2024. All rights reserved.