Previously posted:
Stephen Bullen Posted:
I have a menu item which reads:
"C:\Office95\Office\EXCEL.EXE" "D:\book2.xls" /e/param1/param2
In book2.xls, I have:
Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA"
() As String
Sub Auto_Open()
Dim s As String, i As Integer
s = GetCommandLine
i = InStr(1, s, "/e")
MsgBox Mid(s, i + 2, 200)
End Sub
When I run it, I get a message box saying "/param1/param2". Works for
Excel 95 and 97. The key is to use the (valid) /e parameter, but tack
on your other parameters afterwards. Not that you can't use spaces in
your parameters, even if they are in strings.
http://x46.deja.com/[ST_rn=ps]/getdoc.xp?AN=337413684
http://www.google.com/groups?hl=pl&lr=&ie=UTF-8&oe=UTF-8&[email protected]&rnum=6
Subject: Re: Command line macro
Date: 1998/03/25
Author: Laurent Longre <
[email protected]>
Posting History
Bonjour,
Yes, there is a way (or simply a work-around?) for an auto-start macro to
read the arguments on the command line, at least with Excel 97 -and
certainly XL7, perhaps XL5 with some changes. I've just tested this method,
which seems to work properly.
For instance, assume that you want to read the command line arguments from
an Auto_open sub in the workbook "c:\temp\test.xls" opened by a batch file
(or by a Win95 shortcut).
1. Your command line should look like this one:
start excel c:\temp\test /e/param1/param2/.../paramN
i.e. : after excel.exe, the name of the workbook containing the
Auto_open, then the switch /e **immediately** followed by your own
arguments. These arguments should be separated by "/" and form a continuous
string without spaces.
For instance, if you want to pass the arguments "c:\temp\file1.dbf", "all"
and "exclusive" to Excel, your command-line should look like:
start excel c:\temp\test /e/c:\temp\file1.dbf/all/exclusive
2. In Test.xls, use the API function GetCommandLine (alias
GetCommandLineA in Win95) to get the contents of this command-line string.
You should then parse the string returned by GetCommandLineA, search for the
separators "/" and store each argument in an array. Here is an example of a
such Auto_open sub:
'**************************************************
Option Base 1
Declare Function GetCommandLineA Lib "Kernel32" () As String
Sub Auto_open()
Dim CmdLine As String 'command-line string
Dim Args() As String 'array for storing the parameters
Dim ArgCount As Integer 'number of parameters
Dim Pos1 As Integer, Pos2 As Integer
CmdLine = GetCommandLineA 'get the cmd-line string
On Error Resume Next 'for the wksht-function "Search"
Pos1 = WorksheetFunction.Search("/", CmdLine, 1) + 1
'search "/e"
Pos1 = WorksheetFunction.Search("/", CmdLine, Pos1) + 1 '1st param
Do While Err = 0
Pos2 = WorksheetFunction.Search("/", CmdLine, Pos1)
ArgCount = ArgCount + 1
ReDim Preserve Args(ArgCount)
Args(ArgCount) = Mid(CmdLine, Pos1, _
IIf(Err, Len(CmdLine), Pos2) - Pos1)
MsgBox "Argument " & ArgCount & " : " & Args(ArgCount)
Pos1 = Pos2 + 1
Loop
End Sub
'**************************************************
If you use the command-line above, this Auto_open sub will automatically
store the three arguments ("c:\temp\file1.dbf", "all" and "exclusive") in
the Args() array and display them.
Again, be sure that you don't insert any space between /e and each argument
in the command-line, otherwise it could fail (Excel can believe that these
"pseudo-arguments" are the names of workbooks to open at startup...).
Bon courage,
Laurent