How do I run VBS script from cmd?

I have the following vbs script from Microsoft Support to add an Excel add-in:

Dim oXL As Object
Dim oAddin As Object
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Set oAddin = oXL.AddIns.Add("c:\Program Files\MyApp\MyAddin.xla",  True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing

      

I save the above script to a file called addin.vbs and run it from the command console:

C:\...>cscript addin.vbs

      

I got the following error:

c:\...\addin.vbs(1, 9) Microsoft VBScript compilation error: Expected end of statement

      

Not sure how can I run it from the cmd console?

I am running it from Windows XP.

+2


source to share


2 answers


Visual Basic for Applications (VBA, which is where your code is written) and Visual Basic Scripting Edition (VBS) are not the same language.

The Windows Scripting Host (WSH, i.e. cscript.exe

and wscript.exe

) only handles the Active Scripting languages ​​(in most installations, VBScript and JScript). VBA can only run in the application intended to host it.



Just follow the instructions on the Microsoft support page and add the script to Excel.

+5


source


The error is due to a proposal As Object

. Unlike VBA, VBScript has only one data type Variant

, so you don't specify the data type when declaring a variable. Remove the suggestions As Object

and the script should work fine:



Dim oXL, oAddin
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Set oAddin = oXL.AddIns.Add("c:\Program Files\MyApp\MyAddin.xla",  True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing

      

+3


source







All Articles