|
-
Dec 14th, 2010, 01:19 PM
#1
Thread Starter
Member
[RESOLVED] Write, run VBS within VBA macro?
(Not sure this is exactly the right forum; if not, a nudge in the right direction is appreciated.)
Wondering if there's a better way to do this:
In Word 2007 VBA (running on Vista Pro), I wrote a macro that wrote a VB Script file and then executed it. It worked fine. I used the following code:
Code:
Sub TestWriteScript()
myFileName = "C:\Documents and Settings\user.name\Documents\TestMe.vbs"
myText = "MsgBox " & Chr(34) & "Got it!" & Chr(34)
bolFileGood = WriteFile(myFileName, myText)
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run ("cscript ""C:\Documents and Settings\user.name\Documents\TestMe.vbs""")
End Sub
Public Function WriteFile(ByVal myFileName As String, ByVal myText As String) As Boolean
On Error GoTo OhWell
hFile = FreeFile
Open myFileName For Output As #hFile
Print #hFile, myText;
Close #hFile
OhWell:
WriteFile = Not CBool(Err.Number)
On Error GoTo 0
End Function
(Note: this was tagged on to another procedure; all variables not Dim'd in the above were declared at the module level.)
I ran into problems at first with the file path in the wshShell.Run line because of the spaces in "Documents and Settings". Then I found out I needed the pairs of double quotes. And so it works.
I also tried setting all the file path and doubled quotes into a string, but couldn't figure that one out?? If I'm going to be able to use this in a distributed code, I'll need to pull the username for the file path and create the string. Or perhaps set an object to the VBS file and run that?
So is there a way to use Run or another method to execute the VB Script from within a macro using a string or object when the file path to the script has spaces in it?
Ed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|