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