excel:如何将.bas文件转换为vbscript / exe或从命令行运行? [英] excel: how to convert .bas file to vbscript/exe or running from command line?

查看:474
本文介绍了excel:如何将.bas文件转换为vbscript / exe或从命令行运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将.bas文件转换为vbscript / exe或从命令行运行?
我在Excel中通过MS Visual Basic for Aplications做了脚本,但是我只能在Excel下运行这个脚本。我如何使这个脚本为.vbs或.exe?

解决方案

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

  Dim xlApp 
设置xlApp = CreateObject(Excel.Application)

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

  Dim i As Integer 
Dim wks as Excel.Worksheet

需要有 As 子句

  Dim i 
Dim wks
pre>



有关两者差异的确切细节,请参阅 INFO:Visual Basic for Applications功能不在VBScript 信息:Visual Basic应用程序中不支持VBScript功能


在WSH下运行代码时,VBA有一个内置的IDE和调试器,但您可以使用Visual Sudio调试脚本文件。 (如果您无法安装VS 2015社区版,则Visual Studio集成的shell也可以正常工作 - 2013 2012年 2010



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

  cscript yourscript.vbs // D // X 

或:

  wscript yourscript.vbs // D // X 

如果您有Office 2007或更早安装,您可以使用Microsoft脚本编辑器进行调试;不需要下载并安装VS.尽管如此,VS的功能远远超过了Microsoft Script Editor和VBA调试器。


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

解决方案

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")

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

need to have the As clause removed:

Dim i
Dim wks


For exact details on the differences between the two, see INFO: Visual Basic for Applications Features Not in VBScript and INFO: VBScript Features Not in Visual Basic for Applications.
VBA has a built-in IDE and debugger which you don't have when running code under WSH, but you can use Visual Sudio to debug the script file. (In the event that you can't install VS 2015 Community Edition, the Visual Studio integrated shells also work -- 2013, 2012, 2010.

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.

这篇关于excel:如何将.bas文件转换为vbscript / exe或从命令行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆