vba excel:如何将 .bas 文件转换为 vbscript/exe 或从命令行运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13800794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:45:03  来源:igfitidea点击:

excel: how to convert .bas file to vbscript/exe or running from command line?

excelexcel-vbavbscriptwshvba

提问by lsv

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 ? ?

如何将 .bas 文件转换为 vbscript/exe 或从命令行运行?我在 Excel 中使用 MS Visual Basic for Application 编写了脚本,但我只能在 Excel 下运行此脚本。我如何将此脚本制作为 .vbs 或 .exe ??

回答by Ciarán

I'm afraid that the short answer is that you can't, well at least not directly. VBA and VBS are slight variant's and while you could save the code into a .vbs file I very much doubt that it would do anything without changing it to VB Script.

我担心简短的回答是你不能,至少不能直接。VBA 和 VBS 是轻微的变体,虽然您可以将代码保存到 .vbs 文件中,但我非常怀疑它是否可以在不将其更改为 VB 脚本的情况下执行任何操作。

To make a .exe you will need something like Visual Studio. Even then you would probably need to rework your code to make it do it's job.

要制作 .exe,您需要像 Visual Studio 这样的东西。即便如此,您也可能需要重新编写代码以使其发挥作用。

I guess it depends on what your code does.

我想这取决于你的代码做什么。

回答by Zev Spitz

I think the simplest option is to save the .basfile with a .vbsextension 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 CreateObjector GetObjectfunctions. (WSH has its own set of built-in objects.) In the case of Excel, you'd need to start with:

我认为最简单的选择是.bas使用.vbs扩展名保存文件并将代码修改为 VBScript;然后在 Windows Script Host (WSH) 下运行它。请记住,在 Excel 下的 VBA 中,您可以访问许多内置对象;在 WSH 下的 VBScript 中,您必须使用或函数自己创建或访问这些对象(请参阅此答案)。(WSH 有自己的一组内置对象。)对于 Excel,您需要从以下内容开始:CreateObjectGetObject

Dim xlApp
Set xlApp = CreateObject("Excel.Application")

Keep in mind that in VBScript, variables do not have a type, so all statements such as:

请记住,在 VBScript 中,变量没有类型,因此所有语句如:

Dim i As Integer
Dim wks As Excel.Worksheet

need to have the Asclause removed:

需要As删除该条款:

Dim i
Dim wks



有关两者之间差异的确切详细信息,请参阅 INFO: Visual Basic for Applications Features Not in VBScript信息:Visual Basic for Applications 功能不在 VBScriptINFO: VBScript Features Not in Visual Basic for Applications信息:Visual Basic for Applications 中没有的 VBScript 功能

VBA 具有在 WSH 下运行代码时没有的内置 IDE 和调试器,但您可以使用 Visual Sudio 来调试脚本文件。(如果您无法安装 VS 2015 Community Edition,Visual Studio 集成 shell 也可以使用——20132013年20122012年20102010 年

Debug your scripts by calling them from the commandline as follows:

通过从命令行调用脚本来调试脚本,如下所示:

cscript yourscript.vbs //D //X

or:

或者:

wscript yourscript.vbs //D //X

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.

如果您安装了 Office 2007 或更早版本,则可以使用 Microsoft 脚本编辑器进行调试;无需下载和安装 VS。然而,VS 远比 Microsoft 脚本编辑器和 VBA 调试器强大。

回答by Christopher J. Scharer

I think everyone has made good points here, but one thing I did not see that would be a big issue is that with VBA and Visual Basic you can use the On Error GoTo command and set an Error line using something such as MainErrorHandler: at the bottom of the function. VB Script does not allow this and you need to use On Error GoTo 0 or On Error Resume next which are both allowed in VBA and Visual Basic. the error handling in VB Script however is allowed in all VB languages.

我认为每个人都在这里提出了很好的观点,但我没有看到这将是一个大问题的一件事是,使用 VBA 和 Visual Basic,您可以使用 On Error GoTo 命令并使用诸如 MainErrorHandler 之类的东西设置错误行:函数的底部。VB Script 不允许这样做,您需要使用 On Error GoTo 0 或 On Error Resume next,这在 VBA 和 Visual Basic 中都是允许的。然而,VB 脚本中的错误处理在所有 VB 语言中都是允许的。