64位操作系统中的命令行参数

我有在32位Excel上工作正常的代码。 该代码不适用于64位excel。 当我在64位Excel中复制粘贴声明时没有错误。 声明是红色的,不被识别。

该代码用于检索命令行参数。

所以例如,如果我运行:启动Excel“C:\ GD \ Edu最近\ ParametersProject.xlsm”/p/"kjh%dg.pdf“它将返回string,我可以parsing并确定input参数:”C: \ GD \ Edu Recent \ ParametersProject.xlsm“/p/"kjh%dg.pdf”

我将如何进行转变?

这里是代码:

'I declared this code in a module called Parameters: Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long) Public Function CmdLineToStr() As String ' ' Returns the command line in the call to Excel ' Dim Buffer() As Byte Dim StrLen As Long Dim CmdPtr As Long CmdPtr = Parameters.GetCommandLine() If CmdPtr > 0 Then StrLen = lstrlenW(CmdPtr) * 2 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 

和…

 'I declared this code in the Workbook open: Sub workBook_open() MsgBox Parameters.CmdLineToStr End Sub 

使用PtrSafe关键字声明语句是推荐的语法。 在64位Declare语句中的例子:

 Declare PtrSafe Function GetActiveWindow Lib "User32" () As LongPtr 

查看这个文档了解更多信息:
https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/declare-statement