64 位命令行参数 os

Command line arguments in 64 bit os

我的代码在 32 位 Excel 上运行良好。 该代码不适用于 64 位 excel。 当我在 64 位 Excel 中复制粘贴声明时没有给出错误。 声明为红色,无法识别。

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

例如,如果我 运行: 开始 Excel "C:\GD\Edu Recent\ParametersProject.xlsm" /p/"kjh%dg.pdf" 我可以解析并确定输入参数的 return 字符串: "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