Passing arguments from command line in excel 2010

I found some code snippets that allow me to pass arguments to excel from the command line.

The code below is placed in a new module with 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

      

and then in this workbook I call this code

Sub workBook_open()
    MsgBox Parameters.CmdLineToStr
End Sub

      

It doesn't work with the function GetCommandLine

, so does the error due to dll linking issues or is it because I have some macros stored in personal.xlsb?

I am calling an excel sheet from the command line with this line: C:\Users\kim\Desktop>start excel Parameters.xlsm /e/nmbnmbmnb

and I am getting this error:

eroor

External procedure

+3


source to share


1 answer


Change Public CmdLineToStr() As String

to Public Function CmdLineToStr() As String



Public CmdLineToStr() As String

is not a procedure, you need to put either Sub

or Function

, so this is a procedure. Hence, the error message "Invalid external procedure" appears because you were definitely outside the procedure.

+3


source







All Articles