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

Comment in-line about halfway down...

Karl E. Peterson wrote:
Ed wrote:
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


Part of the problem is in the parameter you pass to wsh.Exec. Because the
whole thing is enclosed in one pair of quotes, you're getting the literal
strings "FPath", "str" and "x" in the command, instead of the values of the
intended variables. Also, because the path contains spaces, the whole
path\filename needs to be enclosed in quotes, represented by chr(34). The
syntax should be [all on one line]

Set proc = wsh.Exec("cscript " & chr(34) & FPath & "MacroTest.vbs" & chr(34)
& " " & str & " " & x)

Be careful to get the spaces and quotes in the right places so the command
processor doesn't barf on an invalid file name.

You also need Karl's modification of the script to call the function, since
all you're passing are the filename and the arguments.


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?


Heh, VBScript takes patience, and the inate ability to break things
up into the smallest possible chunks. Try running the script
directly from the command line, first, for example, to determine what
it's doing. Going back to that:

Sub TestMe(str, x)

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

End Sub


You're not calling the TestMe subroutine? Try rewriting like this:

----------------------------
Call TestMe

Sub TestMe()
Set Args = WScript.Arguments
This = Args(0)
That = Args(1)
wscript.echo This
wscript.echo That
End Sub
----------------------------

Then, at the command prompt (beware word-warp):

C:\Documents and Settings\edward.millis\My Documents\Scriptingcscript
MacroTest.vbs ABC 2

You should see your parameters echoed at this point.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


 
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 06:02 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"