为什么"壳"在VS6的VBscript中不起作用?

时间:2020-03-05 18:41:35  来源:igfitidea点击:

在Visual Studio 6的宏中,我想运行一个外部程序,因此输入:

shell("p4 open " + ActiveDocument.FullName)

这给了我一个类型不匹配的运行时错误。我最终不得不输入的是:

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

这里发生了什么?那个废话真的必要吗,或者我错过了什么?

解决方案

回答

VBScript不是Visual Basic。

回答

试试看:

Shell "p4 open" & ActiveDocument.FullName

回答

正如lassevk指出的那样,VBScript不是Visual Basic。

我相信VBScript中唯一的内置对象是WScript对象。

WScript.Echo "Hello, World!"

来自文档

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.

其他所有内容都必须通过CreateObject调用来创建。这些对象中的一些在此处列出。

如果要在对象上调用方法,则Shell对象是需要创建的其他对象之一。

需要注意的是RegExp是内置的,我们可以像在VBScript中那样实例化RegExp对象:

Dim r as New RegExp

回答

VB6使用&而不是+来连接字符串,并且我们需要确保文件名用空格括起来。像这样尝试:

Shell "p4 open """ & ActiveDocument.FullName & """"