我有一段代码,用于在打开Excel工作簿(64位;但32位代码也存在#if子句下)时获取命令行参数。
例如,当我在命令提示符下运行以下代码行时,我期望能够将输入字符串作为命令行参数获取:
start Excel ".\AwajiPush.xlsm" /p/"kjh%dg.pdf"
(顺便说一下,“start”之所以存在,是为了使它能在.bat批处理文件中工作)
我期望能够捕获".\AwajiPush.xlsm"和/p/"kjh%dg.pdf"作为参数。
但实际上这段代码并没有做到这一点。
为什么它不能获取第二个参数?
我对指针的工作原理不是很了解。是否有一段代码可以用来至少捕获包含两个参数的字符串,以便我可以解析它。如果它包含更多内容,那也没关系。只要它是一致的,我就可以解释它。
我在程序中放置了存根(MsgBox),但我不确定为什么第二个存根显示为空白。
以下是代码:
并且
例如,当我在命令提示符下运行以下代码行时,我期望能够将输入字符串作为命令行参数获取:
start Excel ".\AwajiPush.xlsm" /p/"kjh%dg.pdf"
(顺便说一下,“start”之所以存在,是为了使它能在.bat批处理文件中工作)
我期望能够捕获".\AwajiPush.xlsm"和/p/"kjh%dg.pdf"作为参数。
但实际上这段代码并没有做到这一点。
为什么它不能获取第二个参数?
我对指针的工作原理不是很了解。是否有一段代码可以用来至少捕获包含两个参数的字符串,以便我可以解析它。如果它包含更多内容,那也没关系。只要它是一致的,我就可以解释它。
我在程序中放置了存根(MsgBox),但我不确定为什么第二个存根显示为空白。
以下是代码:
'Put this code in a new module called Parameters
Option Explicit
#If Win64 Then
Private Declare PtrSafe Function GetCommandLineL Lib "kernel32" _
Alias "GetCommandLineA" () As LongPtr
Private Declare PtrSafe Function lstrcpyL Lib "kernel32" _
Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As LongPtr) As Long
Private Declare PtrSafe Function lstrlenL Lib "kernel32" _
Alias "lstrlenA" (ByVal lpString As LongPtr) As Long
#Else
Private Declare Function GetCommandLineL Lib "kernel32" _
Alias "GetCommandLineA" () As Long
Private Declare Function lstrcpyL Lib "kernel32" _
Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lstrlenL Lib "kernel32" _
Alias "lstrlenA" (ByVal lpString As Long) As Long
#End If
Function GetCommandLine() As String
Dim strReturn As String
#If Win64 Then
Dim lngPtr As LongPtr
#Else
Dim lngPtr As Long
#End If
Dim StringLength As Long
'Get the pointer to the commandline string
lngPtr = GetCommandLineL
'get the length of the string (not including the terminating null character):
StringLength = lstrlenL(lngPtr)
MsgBox StringLength
'initialize our string so it has enough characters including the null character:
strReturn = String$(StringLength + 1, 0)
'copy the string we have a pointer to into our new string:
MsgBox strReturn
lstrcpyL strReturn, lngPtr
'now strip off the null character at the end:
MsgBox strReturn
GetCommandLine = Left$(strReturn, StringLength)
End Function
并且
'Put this code in "This Workbook"
Sub workBook_open()
MsgBox Parameters.GetCommandLine
End Sub
set
命令创建一个环境变量,那么 VBA 的Environ
函数将无法返回该变量。它会返回一个空字符串。例如,在命令提示符处执行以下操作:set test=abc
。 - johny why/p
之后的所有内容。但是我们必须使用/p
吗?我们可以使用除/
或p
以外的其他字符或字母吗? - johny why