One option is to use a command line parameter to specify which module Main
method to call. I found some code to retrieve the command line here.
Create a new module and add this code:
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
In you Workbook_Open
method, retrieve the command line and check the parameters:
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
CmdRaw = GetCommandLine
CmdLine = CmdToSTr(CmdRaw)
If InStr(CmdLine, "Main2") > 0 Then
Module2.Main
Else
Module1.Main
End If
End Sub
In you batch file, run Excel like this:
EXCEL.EXE C:\MyFolder\MyWorkbook.xlsm /e /Main2