在Excel 2010中传递来自命令行的参数

我已经find了几个代码片断,允许我从命令行传递参数到excel中。

下面的代码放在一个名为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 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 = GetCommandLineW() 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 Sub 

然后在这个工作簿中我称这个代码

 Sub workBook_open() MsgBox Parameters.CmdLineToStr End Sub 

它与GetCommandLine函数失败,所以是由于链接DLL库问题,或者这是由于我有一些macros存储在personal.xlsb?

我使用下面的命令行来调用excel表格: C:\Users\kim\Desktop>start excel Parameters.xlsm /e/nmbnmbmnb

我得到这个错误:

eroor

外部程序

Public CmdLineToStr() As String更改Public CmdLineToStr() As String

Public Function CmdLineToStr() As String

Public CmdLineToStr() As String不是一个过程,你需要Public CmdLineToStr() As StringFunction所以它是一个过程。 因此,错误消息“无效的外部过程”,因为你正是在程序之外。