How to Run VBS script from cmd? - vbscript

I have the following vbs script from Microsoft Support for adding 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 as addin.vbs and run it from a 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 I can run it from cmd console?
I am running it from Windows XP.

Visual Basic for Applications (VBA, which your code is written in) and Visual Basic Scripting Edition (VBS) are not the same language.
Windows Scripting Host (WSH, i.e. cscript.exe and wscript.exe) only handles Active Scripting languages (in most installs, VBScript and JScript). VBA can only be run within the application that is destined to host it.
Just follow the directions on the Microsoft Support page you have and add the script to Excel.

The error occurs because of the As Object clause. Unlike VBA, VBScript has only one data type — Variant, so you don't specify the data type when declaring a variable. Remove the As Object clauses 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

Related

Set/adjust file version of a rtf-file with VBscript

I created a logon script to automatically create a email signature for Outlook. This script creates the following file "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures\Default_signature.rtf"
I would like to add to a versionnumber to this file so I can check if an update of this file is necessary or not. If not, exit logon script. Else update signature with a higher versionnumber.
To get a versionnumber was pretty easy to find, but I couldn't find how to set/change a versionnumber with VBscript.
Using the DSOFile.OleDocumentProperties didn't work for me. I kept getting a ActiveX-error "Can't create object". It could be that I have to register
the dsofile.dll in the system but this code has to work for every user in our company and I don't want to install this on every device before I can use this logon script.
'Code for requesting versionnumber
Set objFSO = CreateObject("Scripting.FileSystemObject")
file = "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures\Default_signature.rtf"
Wscript.Echo "Version = " & objFSO.GetFileVersion(file)
I hope someone could tell me how to do this :)
As far as i know DSO is the only supported way by Microsoft to change properties of the files without rewriting them. There are some VBA scripts out there for Excel, but that does not apply in your case with RTFs.
However, might i suggest a different alternative? I have in my infrastructure a script that is placed to run at each logon and checks on a share a certain file's last modified date. If the file present on the machine is older, then it's replaced by that on the share.
Dim objFSO, strFileName
strFileName = "C:\Users\user\Desktop\Tests\fdsfsd.rtf"
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
WScript.Echo objFSO.GetFile( strFileName ).DateLastModified
Set objFSO = Nothing

Expected Statement error when using multiple parameters in VBS

I am working on making a simple vbs that will open and close an excel file and save as it closes. This is to update the values inside the excel file after they are modified by a separate python script, and I need to do it this way for many dumb reasons outside my control.
I don't have a lot of experience with VBS but what I researched about this, I don't see the error in my code. However, I get an error whenever I try to run the code below. There error is:
Line: 7
Char: 39
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript compilation error
Any help would be appreciated, thanks!
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
if(fso.FileExists("C:\Users\Public\Documents\Templates\testfile.xlsx")) then
set xlapp = createobject("Excel.Application")
xlapp.Workbooks.Open "C:\Users\Public\Documents\Templates\testfile.xlsx"
xlapp.ActiveWorkbook.SaveAs Filename:="C:\Users\Public\Documents\Templates\testfile_test.xlsx", FileFormat:=51, ConflictResolution:=2
xlapp.ActiveWorkbook.Close SaveChanges:=True
end if
If you are using standalone VBS, the following lines won't work because VBS isn't VBA. VBA supports Named Argument Syntax in function calls whereas its not supported by VBS
xlapp.ActiveWorkbook.SaveAs Filename:="C:\Users\Public\Documents\Templates\testfile_test.xlsx", FileFormat:=51, ConflictResolution:=2
xlapp.ActiveWorkbook.Close SaveChanges:=True
You have to provide the arguments to the function. So Function ArgName:=ArgValue becomes Function ArgValue
xlapp.ActiveWorkbook.SaveAs "C:\Users\Public\Documents\Templates\testfile_test.xlsx",51,,,,,,2
xlapp.ActiveWorkbook.Close True

InstallShield - Using vbscript to write to registry

The following code is a peace of a script that is set up to be executed by InstallShield when installing my application: Custom Actions During Installation > After Register Product
Set wsShell = CreateObject("WScript.Shell")
wsShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\My Program\FilePath", "{the file path}"
When I run the script from command line it runs as it should and writes to the registry. But when I try to use the setup file it shows no errors, but the registry value is not set.
Why is it not working?
I'm running it as administrator.
You can use this code to log information in you msi log, it might help you figure out what is happening
Function LogMSIInfo(msg,msgtype)
Dim rec
Set rec = Session.Installer.CreateRecord(1)
rec.StringData(0) = msg
LogMSIInfo = Session.Message(&H04000000, rec)
End Function

Run VBScript using CFExecute throws error but works fine via command line

I am trying to run a VBScript but CFExecute throws an error
<cfexecute name = "C:\Windows\System32\CScript.exe"
arguments = "//NoLogo D:\Excel.vbs D:\test.xls"
variable = "data"
timeout = "100">
</cfexecute>
<cfdump var="#data#">
Error:
Error: 424 Source: Microsoft VBScript runtime error Description: Object required
But when I run the VBScript with CMD it works fine
C:\Windows\System32 > cscript //nologo D:\Excel.vbs D:\test.xls
I have full admin access, so why am I getting this error?
It was due to bug in the Windows 2008 server.
For office automation (accessing via script and non-window based operation) we have to add a "Desktop" folder inside
C:\Windows\System32\config\systemprofile
C:\Windows\SysWOW64\config\system32
I added it and found success.
Create a vbscirpt file (.vbs). The code content would have the task you want to achieve.
The below example contains the vbscript file, that refreshes the excel and the cfm which executes the vbscript.
Sample vbscript file code:-
Set fso = CreateObject("Scripting.FileSystemObject")
Set xl = CreateObject("Excel.Application")
xl.Visible = True
For Each f In fso.GetFolder("C:\inetpub\WebSites\Upload\").Files
If LCase(fso.GetExtensionName(f.Name)) = "xlsx" Then
Set wb = xl.Workbooks.Open(f.Path)
wb.RefreshAll
wb.Save
wb.Close
End If
Next
xl.Quit
Sample cfm file code:-
<cfexecute name = "C:\Windows\System32\cscript.exe" arguments = "C:\inetpub\WebSites\CSAT\test.vbs">
</cfexecute>

Alias error in VBS

How to use alias in VBScript? I am trying following code:
AliasesExample
Sub AliasesExample
Dim AliasObj
' Obtains the object that corresponds to the Notepad main window
Set AliasObj = Aliases.notepad.wndNotepad
' Checks whether the specified window exists
If AliasObj.Exists Then
' Enters text in the Notepad editor
AliasObj.Keys("Hello, world.")
Else
Log.Error("Notepad is not running.")
End If
End Sub
but getting the following error:
object required: 'Aliases'
VBScript and Windows Script Host do not support calling DLL and Windows API functions, but there're some possible solutions:
You can call DLL functions that are exposed through COM objects:
Set obj = CreateObject("Foo.Bar")
Call obj.Method(Param1, Param2)
You may be able to call some DLL functions using rundll32 if the DLL and the function meet certain requirements.
' Open "Programs and Features" using the Control_RunDLL function from shell32.dll
Set oShell = CreateObject("WScript.Shell")
oShell.Run "rundll32.exe shell32.dll Control_RunDLL appwiz.cpl,,0"
Other than that, you're out of luck.
So generally, you need a COM-callable wrapper for your DLL function to be able to use it from VBScript.

Resources