help with vb script

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
SuitedAces
Pro Scripter
Posts: 50
Joined: Thu Feb 21, 2008 9:11 pm

help with vb script

Post by SuitedAces » Mon Feb 25, 2008 9:03 pm

I'm using this script from the forum to run a macro in excel.

//Put this VBScript block at top of script
VBSTART
Sub OpenAndRun(xlFileName, MacroName)
Set xlApp = CreateObject("Excel.Application")
'remove next line to make Excel invisible
xlApp.visible = true
Set xlBook = xlApp.WorkBooks.Open(xlFileName)
xlApp.Run(MacroName)
'Uncomment next line to quit Excel
'xlApp.quit
End Sub
VBEND

//Use this line when you want to run the Excel macro:
VBRun>OpenAndRun,c:\files\myexcel.xls,MyMacro

How can I get a return variable back ?
Is the any way to run the macro as I am in the code above from within a function.
The function then takes a value that is placed on the worksheet by the excel macro, which then allows me to use VBEval ?

Is that possible ?

Or do I need to do a seperate DDE request ?
If so is there anyway to do that with excel hidden like in the above code ?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Feb 25, 2008 9:15 pm

You mean get a variable back from Excel?

The function call can't do that. Probably best way would be to have Excel put the value in a cell and then use a simple DDE call or VBscript to read the cell.

That code is just an example that creates an instance of Exce, opens an xls file and then runs the macro. You could rearrange that code however you like, and have the bit that runs the macro wherever you want.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

SuitedAces
Pro Scripter
Posts: 50
Joined: Thu Feb 21, 2008 9:11 pm

Post by SuitedAces » Mon Feb 25, 2008 10:12 pm

Yes but can I do the DDE with excel hidden ?

Do I need to complete this macro first and do a DDE after I close ?
Or can I use the code above edit out the close app command, do a DDE and then run the same code for the sole purpose of closing my excel file ?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Feb 25, 2008 10:29 pm

I would use VBScript to read the cell before the close. See link I posted earlier on how to read Excel data. That article has VBscript for reading data from Excel. There's also a sample script that comes with Macro Scheduler with same code. Insert code that reads the data between the run and the close.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts