Step.1 Create a macro-enabled workbook
Step.2 Open the workbook, and open the VBA, insert a moudle, copy and paste the following code sample in the module
Option Base 0
Option Explicit
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)
Function CmdToSTr(Cmd As Long) As String
Dim Buffer() As Byte
Dim StrLen As Long
If Cmd Then
StrLen = lstrlenW(Cmd) * 2
If StrLen Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal Cmd, StrLen
CmdToSTr = Buffer
End If
End If
End Function
Step.4 Copy and paste the following code sample into ThisWorkbook
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
Dim myParam As String
CmdRaw = GetCommandLine
CmdLine = CmdToSTr(CmdRaw)
myParam = Right(CmdLine, 6)
MsgBox myParam
End Sub
Step.4 Sign your code or modify the macro security settings to allow the macro to run automatically
Step.5 Save and close the workbook
Step.6 Run command line with the parameter, for example:
Excel "Server\ShareFolder\TheWorkbook.xlsm" /e/myParam
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The code sample above will pass the last 6 characters (modify the code if you need more or less) to the workbook
- The workbook will display a message box to display the parameter we passed from the command line, add the other function as you need
The code sample refers to the reply from Air_Cooled_Nut in
http://www.vbforums.com/showthread.php?366559-Excel-How-to-Pass-Command-Line-Parameter-DKenny-is-KING!