LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.scripting.vbscript,microsoft.public.excel.programming,microsoft.public.word.vba.general
Ed Ed is offline
external usenet poster
 
Posts: 65
Default Call VBScript from VBA, pass variable, return result to macro?

Hi, Karl. No, this is only for me. I'm not about to unleash my messes
on the world at large!! <g

Okay - I'm trying to take this one step at a time. I've got a vbs file
with

Sub TestMe(str, x)

Set Args = WScript.Arguments
This = Args(0)
That = Args(1)
wscript.echo This
wscript.echo That

End Sub

and I've got a Word macro with

Sub TestMyScriptHere()

Dim str As String
Dim x As Long
str = "ABC"
x = 2

Dim FPath As String
FPath = "C:\Documents and Settings\edward.millis\My
Documents\Scripting\"

Dim wsh, proc

Set wsh = CreateObject("WScript.Shell")
Set proc = wsh.Exec("cscript FPath & MacroTest.vbs str x")

End Sub

I ran it, and got a brief flash of a command prompt window (Word
&Windows XP) - no "echo" message boxes. So I at least got the macro to
open the script file. I didn't get the variables into the script,
though. Is there hope for me? Do I need years of intnse training? Or
will a small drop-kick in the right direction get me going?

Ed

Karl E. Peterson wrote:
Ed wrote:
I've been looking at newsgroup posts and VBScript sites all morning,
and can't seem to find a simple explanation and method for calling a
script from a Word or Excel macro and passing a variable into the
script, then returning the result of whatever the script does into the
macro.

That can mean there isn't any simple way to do this, and I need to
stop looking. In the hopes, however, that I am too unlearned to
recognize the answer when I see it, I'd like to just ask: how do I do
this?


Simple? It depends. Are you planning to "ship" this to computers outside
your control? If not, yeah, there's a way that isn't overly involved.
You'll, essentially, have to "lower yourself" to using VBS within your VBA.
Fire off the VBS scripts with something like ("air code" alert! <g) this:

Set wsh = CreateObject("WScript.Shell")
Set proc = wsh.Exec("cscript your.vbs param1 param2")

Spin cycles in your VBA app, waiting for the VBS to complete:

' Wait for application to exit
Do While proc.Status = 0
Sleep 1 'Need to add the API declaration to your module!
Loop

Retrieve the command line parameters within the VBS like this (actual
example taken from a script I use here):

' http://www.winguides.com/scripting/reference.php?id=117
Set Args = WScript.Arguments
If Args.Count = 2 Then
' First argument is username; Second is password
User = LCase(Args(0))
Pass = Args(1)
WScript.Echo "Login as: " & User
Else
User = LCase(Network.UserName)
End If

Have your VBS use the optional ErrorCode parameter to the Quit method to
send a return value:

http://www.winguides.com/scripting/reference.php?id=111

Read that value with:

' http://www.codecomments.com/archive2...-1-335409.html
Debug.Print "VBS Return Value: "; proc.ExitCode

Hope this helps!
--
Working without a .NET?
http://classicvb.org/


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to return the result of a cell's formula as an argument within a running macro? Worldman1 Excel Programming 4 May 26th 06 06:59 AM
Pass variable to macro Jason Morin Excel Programming 2 November 28th 05 04:13 PM
Define worksheet as variable, then call in macro bugman Excel Programming 2 November 22nd 05 05:13 PM
Can you call VBScript code from VBA? John Keith[_2_] Excel Programming 1 June 15th 05 12:18 AM
Pass variable from user form to macro r wilcox Excel Programming 1 May 23rd 05 07:27 PM


All times are GMT +1. The time now is 08:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"