在Excel VBA中访问命令行数据失败
我需要访问Excel VBAmacros中的命令行参数,并发现许多变体,但只有一个与Excel-2010一起工作,似乎API随时间而改变。 我试过这个代码,我发现“ 在那里 ”:
Dim pCmdLine As Long ' Pointer to the Comand-line string Dim strCmdLine As String ' Command line string Dim CmdLine As String ' Command line string ' Get the pointer to the command line string pCmdLine = GetCommandLineA ' Fill the string with zeros ' (300 characters for command line seems to be enough) strCmdLine = String$(300, vbNullChar) ' Copy from the pointer to VBA-style string lstrcpynA strCmdLine, pCmdLine, Len(strCmdLine) ' At this point we got the string, ' now skip the rest of it filled with 0 characters. CmdLine = Left(strCmdLine, InStr(1, strCmdLine, vbNullChar) - 1) MsgBox "Length of the command line = " & Len(CmdLine) '' Debug MsgBox "Command Line:: " & CmdLine '' Debug
我把它放到电子表格的Auto_openmacros中。 如果我尝试这个电话:
start excel TestMacro.xlsm /e/abcd/xyz
它似乎一般工作和macros观报告:
Command line = " C:/.../excel.exe TestMacro.xlsm"
所以我得到了调用部分,但参数丢失。
部分解决scheme:我发现如果我改变调用:
start excel /e/abcd/xyz TestMacro.xlsm
它的工作原理,除了parsing代码必须改变,忽略不是最后的文件名,而且这种forms似乎不允许在任何参数,即使引用任何空白。 系统似乎将它们解释为目标excel文件的文件名,并给出错误。 例如:
start excel /e/abc/"my sheet"/ TestMacro.xlsm
给出错误:
file 'sheet"/.xlsx' not found
虽然在虚假的启动错误之后,打算的表单确实打开,并且整条线路都可以工作。