问题描述
如何将 .bas 文件转换为 vbscript/exe 或从命令行运行?我在 Excel 中使用 MS Visual Basic for Application 编写了脚本,但我只能在 Excel 下运行此脚本.我如何将此脚本制作为 .vbs 或 .exe ??
How to convert .bas file to vbscript/exe or running from command line ?I did script in Excel by MS Visual Basic for Aplications, but i can run this scrip only under Excel. How i can make this script as .vbs or .exe ? ?
推荐答案
我认为最简单的选择是使用 .vbs
扩展名保存 .bas
文件并修改您的代码到 VBScript;然后在 Windows Script Host (WSH) 下运行它.请记住,在 Excel 下的 VBA 中,您可以访问许多内置对象;在 WSH 下的 VBScript 中,您必须使用 CreateObject 自己创建或访问这些对象(请参阅此答案)
或 GetObject
函数.(WSH 有自己的一组内置对象.)对于 Excel,您需要从以下内容开始:
I think the simplest option is to save the .bas
file with a .vbs
extension and modify your code to VBScript; then run it under Windows Script Host (WSH). Keep in mind that in VBA under Excel you have access to a number of built-in objects; in VBScript under WSH you'll have to create or access those objects yourself (see this answer) with the CreateObject
or GetObject
functions. (WSH has its own set of built-in objects.) In the case of Excel, you'd need to start with:
Dim xlApp
Set xlApp = CreateObject("Excel.Application")
请记住,在 VBScript 中,变量没有类型,因此所有语句如:
Keep in mind that in VBScript, variables do not have a type, so all statements such as:
Dim i As Integer
Dim wks As Excel.Worksheet
需要删除 As
子句:
Dim i
Dim wks
有关两者之间差异的确切详细信息,请参阅 信息:Visual Basic for Applications 功能不在 VBScript 中 和 信息:Visual Basic for Applications 中没有的 VBScript 功能.VBA 具有在 WSH 下运行代码时没有的内置 IDE 和调试器,但您可以使用 Visual Sudio 来调试脚本文件.(如果您无法安装 VS 2015 社区版,Visual Studio 集成外壳也可以使用 -- 2013 年,2012,2010.
通过从命令行调用脚本来调试脚本,如下所示:
Debug your scripts by calling them from the commandline as follows:
cscript yourscript.vbs //D //X
或:
wscript yourscript.vbs //D //X
如果您安装了 Office 2007 或更早版本,则可以使用 Microsoft 脚本编辑器进行调试;无需下载和安装 VS.尽管如此,VS 远比 Microsoft 脚本编辑器和 VBA 调试器强大得多.
If you have Office 2007 or earlier installed, you can use the Microsoft Script Editor for debugging; there's no need to download and install VS. Nevertheless, VS is far more powerful than both Microsoft Script Editor and the VBA debugger.
这篇关于excel:如何将 .bas 文件转换为 vbscript/exe 或从命令行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!