Excel: how to convert a .bas file to vbscript / exe or run from the command line?

How to convert the .bas file to vbscript / exe or run from the command line? I made a script in Excel using MS Visual Basic for Aplications, but I can run this script only in Excel. How can I make this script as .vbs or .exe?

+2
source share
3 answers

I think the easiest option is to save the .bas file with the .vbs extension and change 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 will need to create or access these objects yourself (see this answer ) using the CreateObject or GetObject . (WSH has its own set of built-in objects.) In the case of Excel, you need to start with:

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

Keep in mind that in VBScript variables are not of type, therefore all statements, such as:

 Dim i As Integer Dim wks As Excel.Worksheet 

you must delete the As clause:

 Dim i Dim wks 


For exact information on the differences between them, see INFO: Visual Basic for Applications Features Not in VBScript and INFO: VBScript Functions Not in Visual Basic for Applications .
VBA has a built-in IDE and debugger that you donโ€™t have when running code under WSH, but you can use Visual Sudio to debug the Script file. (If you cannot install VS 2015 Community Edition, the integrated shells of Visual Studio - 2013 , 2012 , 2010 will also be integrated.

Debug your scripts by invoking them from the command line 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; No need to download and install VS. However, VS is much more powerful than Microsoft Script Editor and the VBA debugger.

+3
source

I am afraid that the short answer is that you cannot, well, at least not directly. VBA and VBS are a small option, and although you can save the code in a .vbs file, I doubt very much that it will do anything without changing it to VB Script.

To create .exe, you need something like Visual Studio. Even then, you may have to redo your code to do the job.

I think it depends on what your code does.

+3
source

I think everyone here had some good points, but one thing that I have not seen would be a big problem, is that with VBA and Visual Basic you can use the On Error GoTo command and set the error line using that- something like 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, which are allowed in VBA and Visual Basic. however, error handling in VB Script is allowed in all VB languages.

0
source

All Articles