[英]EXCEL 64 bit command line vba code
I have code to fetch command line arguments when opening up excel book (64 bit; but 32 bit code is also there under #if clause). 我打开excel book时有代码来获取命令行参数(64位;但#if子句下也有32位代码)。
So for example, when I run the following line of code at the command prompt, I am expecting to be able to fetch the input string as the command line arguments: 例如,当我在命令提示符下运行以下代码行时,我希望能够将输入字符串作为命令行参数获取:
start Excel ".\\AwajiPush.xlsm" /p/"kjh%dg.pdf" 启动Excel“。\\ AwajiPush.xlsm”/p/"kjh%dg.pdf“
(By the way, the reason why "start" is there, is so that it would work in a .bat batch file) (顺便说一下,“start”之所以存在,是因为它可以在.bat批处理文件中工作)
I am expecting to be able to capture ".\\AwajiPush.xlsm" and /p/"kjh%dg.pdf" as parameters. 我希望能够捕获“。\\ AwajiPush.xlsm”和/p/"kjh%dg.pdf“作为参数。
The code doesn't do that. 代码不这样做。
Why is it not fetching 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 that contains both parameters, so that I can parse it. 是否有一段代码可用于捕获至少包含两个参数的字符串,以便我可以解析它。 If it contains more, that is fine. 如果它包含更多,那很好。 I can always interpret it, as long as it is consistent. 只要它是一致的,我总能解释它。
I put stubs in the program (MsgBox), and I am not sure why the second stub shows blank. 我把存根放在程序(MsgBox)中,我不知道为什么第二个存根显示为空白。
Here is the code: 这是代码:
'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
And 和
'Put this code in "This Workbook"
Sub workBook_open()
MsgBox Parameters.GetCommandLine
End Sub
Here's a function to get the command line from the current process: 这是从当前进程获取命令行的函数:
Private Declare PtrSafe Function w_commandline Lib "kernel32.dll" Alias "GetCommandLineW" () As LongPtr
Private Declare PtrSafe Function w_strlen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As LongPtr) As Long
Private Declare PtrSafe Sub w_memcpy Lib "kernel32.dll" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal size As LongPtr)
Public Function GetCommandLine() As String
GetCommandLine = String$(w_strlen(w_commandline()), 0)
w_memcpy ByVal StrPtr(GetCommandLine), ByVal w_commandline(), LenB(GetCommandLine)
End Function
Sub Test()
Debug.Print GetCommandLine()
End Sub
Note that you'll have to use the /e
switch to avoid a redirection to an already launched instance of Excel and thus to keep the provided parameters. 请注意,您必须使用/e
开关来避免重定向到已启动的Excel实例,从而保留提供的参数。 For example: 例如:
excel.exe /e "C:\temp\myfile.xlsm" /p "myparam"
Or with start
: 或者start
:
start "xl" excel.exe /e "C:\temp\myfile.xlsm" /p "myparam"
But if your goal is to provide some arguments to VBA from a batch, then use an environment variable: 但是,如果您的目标是从批处理中为VBA提供一些参数,那么请使用环境变量:
Set MyArguments=abcde
start "xl" excel.exe /e "C:\temp\myfile.xlsm"
, then to get the argument from Excel: ,然后从Excel获取参数:
Debug.Print Environ("MyArguments") ' >> "abcde" '
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.