Question

In a macro for Visual Studio 6, I wanted to run an external program, so I typed:

shell("p4 open " + ActiveDocument.FullName)

Which gave me a type mismatch runtime error. What I ended up having to type was this:

Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
strResult = wshShell.Run("p4 open " + ActiveDocument.FullName)

What is going on here? Is that nonsense really necessary or have I missed something?

Was it helpful?

Solution

As lassevk pointed out, VBScript is not Visual Basic.

I believe the only built in object in VBScript is the WScript object.

WScript.Echo "Hello, World!"

From the docs

The WScript object is the root object of the Windows Script Host object model hierarchy. It never needs to be instantiated before invoking its properties and methods, and it is always available from any script file.

Everything else must be created via the CreateObject call. Some of those objects are listed here.

The Shell object is one of the other objects that you need to create if you want to call methods on it.

One caveat, is that RegExp is sort of built in, in that you can instantiate a RegExp object like so in VBScript:

Dim r as New RegExp

OTHER TIPS

VBScript isn't Visual Basic.

Give this a try:

Shell "p4 open" & ActiveDocument.FullName

VB6 uses & to concatenate strings rather than +, and you'll want to make sure the file name is encased in quotes in case of spaces. Try it like this:

Shell "p4 open """ & ActiveDocument.FullName & """"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top