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

时间:2017-12-11 15:29:12

标签: excel vba 64-bit

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

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

例如,如果我跑了: 启动Excel" C:\ GD \ Edu Recent \ ParametersProject.xlsm" /p/"kjh%dg.pdf" 它会返回我可以解析的字符串并确定输入参数: " 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

1 个答案:

答案 0 :(得分:0)

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

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

查看此文档以获取更多信息:
https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/declare-statement