View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Steve Yandl[_2_] Steve Yandl[_2_] is offline
external usenet poster
 
Posts: 37
Default Can you have a command line arg with a workbook?

Trefor,

I've always used a vbScript file as a sort of helper file to do this sort of
thing.

For my test, I created a workbook named "C:\Test\TestBk1.xls". In module 1
I created a subroutine named "TestMacro" that takes two text string
arguments.

Now I create a text file that I name "C:\Test\LaunchSub.vbs". The contents
of this script is shown between the lines below,
________________________________________
arg1 = WScript.Arguments(0)
arg2 = WScript.Arguments(1)


Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objWkbk = objXL.Workbooks.Open("C:\Test\TestBk1.xls")
objXL.run "TestMacro", CStr(arg1), CStr(arg2)
__________________________________________

Now, the command line that I can use at the prompt for cmd.exe or at the
'Start Run' line for Windows would be:
WScript.exe "C:\Test\LaunchSub.vbs" "dog" "cat"
Note, that I am using WScript.exe with the name of my vbs file as the first
argument. After that is a space followed by my first argument to go to the
vbs script and then a second space followed by my final argument. Those
arguments will be retrieved in the WScript.Arguments collection. In the
script, all the arguments will be treated as variant type so you will want
to convert them to the appropriate data type before sending them to your
Excel macro.


Steve Yandl




"Trefor" wrote in message
...
If it possible to read an arg from the command line in VBA?

myworkbook.xls arg1 arg2

I would like to run a certain macro if a certain argument or parameter is
specified.
--
Trefor