I have code to extract command line arguments when opening an excel workbook (64 bit, but 32-bit code also exists in the #if section).
So, for example, when I run the following line of code on the command line, I expect that I can get the input line as command line arguments:
run Excel ". \ AwajiPush.xlsm" / p / "kjh% dg.pdf"
(By the way, the reason there is a “start” is because it will work in a .bat batch file)
I expect that I can capture ". \ AwajiPush.xlsm" and / p / "kjh% dg.pdf" as parameters.
The code does not do this.
Why doesn't he extract the second argument?
I don’t know too much about how pointers work. Is there a piece of code that I can use to capture at least a string containing both parameters so that I can parse it. If it contains more, this is normal. I can always interpret it, if agreed.
I put stubs in the program (MsgBox), and I'm not sure why the second stub shows an empty one.
Here is the code:
'Put this code in a new module called Parameters
Option Explicit
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
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
Function GetCommandLine() As String
Dim strReturn As String
Dim lngPtr As LongPtr
Dim lngPtr As Long
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
and
'Put this code in "This Workbook"
Sub workBook_open()
MsgBox Parameters.GetCommandLine
End Sub
source
share