VBS starts Excel. How to point to different macro when run?

C

CRayF

I have the folloining code in a file called RaceBetting.vbs. When run, it
startes the the RaceBetting.xls and runs the "AutoOpen" macro. I would like
to point it to run a different macro when run. Below the:
XLApp.ActiveWorkbook.RunAutoMacros 1
points to a number. How do I relate this to the macros?
The Macro I want to run is "ImportRaceProgramData()" instead of the
Auto_Open()

---------------------
Dim XLApp
Dim XLWkb
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = true
XLApp.Workbooks.Open "RaceBetting.xls"
XLApp.ActiveWorkbook.RunAutoMacros 1
--------------------
 
R

RB Smissaert

In Excel the syntax is like this:

Application.Run "WhateverWorkbook.xls!ModuleName.ProcedureName", Argument1,
Argument2

So it looks you will need:

XLApp.Run "RaceBetting.xls!ModuleName.ImportRaceProgramData"


RBS
 
C

CRayF

I’m getting a error: C:\xxxxxx\xxxxxRaceBetting.vbs
Line 6 Char 1
The macro ‘RaceBetting.xls!ModuleName.ImportRaceProgramData’ cannot be found…

When I go to Macro’s and edit ImportRaceProgramData
On the Alphabetic (vs Categorized) TAB it is listed as:
(Name) ImportRaceProgramData

On the top left, under the VBAProject(RaceBetting.xls)
Under the Modules TAB I have 2 moduls listed.:
AutoStarted (and)
ImportRaceProgramData

I think ImportRaceProgramData once said Modual1 or something but I though I
changed this by typing over it in the (Name) on the bottom left. As mentioned
above it says ImportRaceProgramData.

-----------------
Dim XLApp
Dim XLWkb
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = true
XLApp.Workbooks.Open "RaceBetting.xls"
XLApp.Run "RaceBetting.xls!ModuleName.ImportRaceProgramData"
 
R

Rowan

I hope to answer this query as well as you last question in "Passing
parms to a VBS file or a macro when executed?" here.

1. I have an Excel file called C:\RaceBetting.xls.

2. This file has a module called ImportRaceProgramData - name changed in
the Properties window.

3. In the ImportRaceProgramData module is a macro called
ImportRaceProgramData ( Personally I would have left the module called
Module1 so as to avoid confusion).

4. The macro looks like this:
Sub ImportraceProgramData(passed_filename)
MsgBox passed_filename
End Sub

5. I have a vbscipt file called c:\RaceBetting.vbs.

6. The script looks like this
------------------------------------------------------------------------
Set WshShell = WScript.CreateObject("WScript.Shell")
passed_name = WScript.Arguments(0)
Dim XLApp
Dim XLWkb
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = true
XLApp.Workbooks.Open "C:\RaceBetting.xls"
XLApp.ActiveWorkbook.RunAutoMacros 1
XLApp.Run _
"RaceBetting.xls!ImportRaceProgramData.ImportRaceProgramData" _
, passed_name
Set XlApp = Nothing
Set WshShell = Nothing
----------------------------------------------------------------------------
Note: I have doubts about this line actually suppressing automacros eg
the Workbook_Open event:
XLApp.ActiveWorkbook.RunAutoMacros 1

7. On the command prompt I enter c:\>RaceBetting.vbs MyFile.xls

8. The excel workbook is opened and a msgbox appears reading MyFile.xls

I hope this helps
Rowan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top