Difference in compilation of a vbcode in cscript command and visual studio - vbscript

I have the following code in visual basic , which I am running through visual studio .
Module Module1
Sub Main()
On Error GoTo errorHandler
Dim oLapp As Object
Dim oItem As Object
oLapp = CreateObject("Outlook.application")
oItem = oLapp.createitem(0)
'
With oItem
.Subject = "A MAIL"
.To = "an_email_id#outlook.com"
.body = "2018 RUSSIA IS OURS"
.Send
End With
Console.Write("KROOOOOOOOSSSSSS")
Console.Read()
'Set oLapp = Nothing
'Set oItem = Nothing
errorhandler:
Console.Write("jkjk")
End Sub
End Module
This code is purported to send an email through outlook . However , I thought of doing this through command prompt and I got to know about "cscript" command which is used to run VB scripts in windows command prompt .
So , I put the following code in an notepad file and saved it with .vbs extension . Then when I tried calling this file using cscript , I got an error .
It said syntax error at line 7 , col23 .
That line turns out to be :
On Error goto Errhandler
As such I commented that line out and tried running again and got an error again in line 8 , stating "expected end of statement .
So , I thought cscript might still be supporting "set" and "let" and hence put set in line 8 and I got the same error again .
So , is there any difference in the compilation of visual basic code in both these places . What will be the equivalent code of the code presented here that can be run via cscript and perform the task of sending an email through outlook .

Related

Launch an external exe and set timeout VBS [duplicate]

I am working on a script with vbscript, and I would like it to terminate itself after x number of minutes.
I was thinking something like grabbing the time when the script starts and then keeping the whole thing in a loop until the time is x number of minutes after the start time, but I need it to keep checking in the background, and not just wait until a loop is complete.
I want a message or something that notifies the user they took too long, which I can do myself.
Is there any way to keep track of the time in the background, or will it be a bit of a drawn-out process to determine it?
Re-launching the script with //T:xx as suggested by Ekkehard.Horner is probably your best option. Another, slightly different, approach could look like this:
Const Timeout = 4 'minutes
timedOut = False
If WScript.Arguments.Named.Exists("relaunch") Then
'your code here
Else
limit = DateAdd("n", Timeout, Now)
cmd = "wscript.exe """ & WScript.ScriptFullName & """ /relaunch"
Set p = CreateObject("WScript.Shell").Exec(cmd)
Do While p.Status = 0
If Now < limit Then
WScript.Sleep 100
Else
On Error Resume Next 'to ignore "invalid window handle" errors
p.Terminate
On Error Goto 0
timedOut = True
End If
Loop
End If
If timedOut Then WScript.Echo "Script timed out."
You'd still be re-launching the script, but in this case it's your script killing the child process, not the script interpreter.
Here is another short and elegant solution which allows to terminate both the script and the external executable ran asynchronously, via WScript.Timeout
Option Explicit
Dim oSmallWrapperWshExec
WScript.Timeout = 7
Set oSmallWrapperWshExec = New cSmallWrapperWshExec
' Some code here
MsgBox "Waiting timeout" & vbCrLf & vbCrLf & "You may close notepad manually and/or press OK to finish script immediately"
Class cSmallWrapperWshExec
Private oWshShell
Private oWshExec
Private Sub Class_Initialize()
Set oWshShell = CreateObject("WSCript.Shell")
With oWshShell
Set oWshExec = .Exec("notepad")
.PopUp "Launched executable", 2, , 64
End With
End Sub
Private Sub Class_Terminate()
On Error Resume Next
With oWshShell
If oWshExec.Status <> 0 Then
.PopUp "Executable has been already terminated", 2, , 64
Else
oWshExec.Terminate
.PopUp "Terminated executable", 2, , 64
End If
End With
End Sub
End Class
I appreciate all of the answers here, but they are more complicated than I wanted to get in to.
I was very surprised to find out that there is a way to do it built into WScript.
WScript.Timeout = x_seconds
cscript
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
**//T:nn Time out in seconds: Maximum time a script is permitted to run**
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
Update:
To help people who downvote a plain (and to the point) citation of cscript.exe's usage message (how can that be wrong?) to see the light through #PanayotKarabakalov's smoke screen:
The claim:
using //T switch not guarantee real time accuracy
that all 5 Echo command executed, even if the Sleep time between them
is 1.5 second and the //T is set to 4
The evidence:
The script is restarted via:
CreateObject("WScript.Shell").Run "WScript " & _
Chr(34) & WScript.ScriptFullName & _
Chr(34) & " /T:4", 0, False
which does not contain the host-specific //T (as opposed to the script-specific /T) switch.
The (counter) argument:
Whatever way you start the first instance of the script (//T or no //T), the second/relaunched instance will never have a time out and will always run to the bitter end.
If you still have doubts, change the invocation in P.'s script to
CreateObject("WScript.Shell").Run "WScript //T:4 " & _
and try it out.

VB GoTo failing compilation

The below script will fail with an error, which I'm ok with. Right now, I'm trying to work out my On Error GoTo <label> syntax, and it is currently failing with the following error.
Line: 2
Char: 16
Error: Syntax Error
Code: 800A03EA
Source: Microsoft VBScript compilation error
Code below:
Sub ComCheck
On Error GoTo ErrorHandler
Dim fortis
Wscript.Echo("Creating COM object.")
Set fortis = CreateObject("TESTCOM.APPLICATION")
Wscript.Echo("Write Database name.")
Wscript.Echo(fortis.Databases[0].Name)
GoTo ScriptEnd
ErrorHandler:
Wscript.Echo("-------ERROR OCCURRED------")
Wscript.Echo("#" + Err.Number + "::" + Err.Description)
Err.Clear
ScriptEnd:
Wscript.Echo("Script complete.")
End Sub
ComCheck()
This is one of the differences between VB and VBScript: the latter doesn't support the GoTo <label> syntax. The only two possibilities in VBScript are:
On Error Resume Next
and
On Error Goto 0
You use the former to turn off VBScript's own error handling (and presumably handle errors yourself), and the latter to turn on VBScript's error handling (which stops all execution if an error is encountered).

while running vba script through cmd prompt getting error [duplicate]

This question already has an answer here:
While Running the vba script i am getting error Microsoft VBScript runtime error: object required : 'DoCmd'
(1 answer)
Closed 8 years ago.
Hi Any one please help me..
while running the vba script i am getting error--object DoCmd need to create.
My script is given below..
ExecuteInsert
Sub ExecuteInsert()
Dim sheetPath
Dim dbs, DbFullName, acc
Set acc = CreateObject("Access.Application")
DbFullName = "D:\G\Diamond\FINAL MS-Access\Demo\MS-Access project.accdb"
Set dbs = acc.DBEngine.OpenDatabase(DbFullName, False, False)
dbs.Execute "Delete from TempRoadMap"
sheetPath = "C:\Users\270784\Desktop\CSPRV scheduled work - 2014 through 1-26-14.xlsx"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "TempRoadMap", sheetPath, True
MsgBox "Imported Sheet1 from " & sheetPath & " Successfully!"
dbs.Execute "Delete from RoadMap"
dbs.Execute "INSERT INTO [RoadMap] ( Release_Name,SPRF,SPRF_CC,Estimate_Type,PV_Work_ID,SPRF_Name,Estimate_Name,Project_Phase,CSPRV_Status,Scheduling_Status,Impact_Type,Onshore_Staffing_Restriction,Applications,Total_Appl_Estimate,Total_CQA_Estimate,Estimate_Total,Requested_Release,Item_Type,Path) SELECT [TempRoadMap.Release Name], [TempRoadMap.SPRF], [TempRoadMap.Estimate (SPRF-CC)],[TempRoadMap.Estimate Type],[TempRoadMap.PV Work ID],[TempRoadMap.SPRF Name],[TempRoadMap.Estimate Name],[TempRoadMap.Project Phase],[TempRoadMap.CSPRV Status],[TempRoadMap.Scheduling Status],[TempRoadMap.Impact Type],[TempRoadMap.Onshore Staffing Restriction],[TempRoadMap.Applications],[TempRoadMap.Total Appl Estimate],[TempRoadMap.Total CQA Estimate],[TempRoadMap.Estimate Total],[TempRoadMap.Requested Release],[TempRoadMap.Item Type],[TempRoadMap.Path] FROM [TempRoadMap] "
dbs.Close
MsgBox "Done"
End Sub
Assuming that by "...through cmd prompt..." you mean to imply that you are running this script as part of a VBScript file, here are the changes you would want to make:
DoCmd is a child of an Access.Application, and it can only be referenced globally in the scope of an Access database. To reference it in VBScript, you must explicitly use an Access.Application instance, which you have as variable acc. Modify the beginning of the DoCmd line like so:
acc.DoCmd.TransferSpreadsheet
VBScript won't recognize Access constants like acImport or acSpreadsheetTypeExcel97, so you will have to replace them with their actual values. A quick look through msdn reveals that acImport is 0 and acSpreadsheetTypeExcel97 doesn't exist, but acSpreadsheetTypeExcel8 (Excel 97 format) is 8. So now the DoCmd line looks like:
acc.DoCmd.TransferSpreadsheet 0, 8, "TempRoadMap", sheetPath, True
VBScript does not have MsgBox - this is only available through VBA. Instead, you can use Wscript.Echo. For example, the last line would be: WScript.Echo "Done"
If you still have issues after making those changes, please let me know with a comment below.
VBScript certainly does have msgbox and echo is not part of vbscript but of Windows Scripting Host.

Terminate vbscript after x minutes

I am working on a script with vbscript, and I would like it to terminate itself after x number of minutes.
I was thinking something like grabbing the time when the script starts and then keeping the whole thing in a loop until the time is x number of minutes after the start time, but I need it to keep checking in the background, and not just wait until a loop is complete.
I want a message or something that notifies the user they took too long, which I can do myself.
Is there any way to keep track of the time in the background, or will it be a bit of a drawn-out process to determine it?
Re-launching the script with //T:xx as suggested by Ekkehard.Horner is probably your best option. Another, slightly different, approach could look like this:
Const Timeout = 4 'minutes
timedOut = False
If WScript.Arguments.Named.Exists("relaunch") Then
'your code here
Else
limit = DateAdd("n", Timeout, Now)
cmd = "wscript.exe """ & WScript.ScriptFullName & """ /relaunch"
Set p = CreateObject("WScript.Shell").Exec(cmd)
Do While p.Status = 0
If Now < limit Then
WScript.Sleep 100
Else
On Error Resume Next 'to ignore "invalid window handle" errors
p.Terminate
On Error Goto 0
timedOut = True
End If
Loop
End If
If timedOut Then WScript.Echo "Script timed out."
You'd still be re-launching the script, but in this case it's your script killing the child process, not the script interpreter.
Here is another short and elegant solution which allows to terminate both the script and the external executable ran asynchronously, via WScript.Timeout
Option Explicit
Dim oSmallWrapperWshExec
WScript.Timeout = 7
Set oSmallWrapperWshExec = New cSmallWrapperWshExec
' Some code here
MsgBox "Waiting timeout" & vbCrLf & vbCrLf & "You may close notepad manually and/or press OK to finish script immediately"
Class cSmallWrapperWshExec
Private oWshShell
Private oWshExec
Private Sub Class_Initialize()
Set oWshShell = CreateObject("WSCript.Shell")
With oWshShell
Set oWshExec = .Exec("notepad")
.PopUp "Launched executable", 2, , 64
End With
End Sub
Private Sub Class_Terminate()
On Error Resume Next
With oWshShell
If oWshExec.Status <> 0 Then
.PopUp "Executable has been already terminated", 2, , 64
Else
oWshExec.Terminate
.PopUp "Terminated executable", 2, , 64
End If
End With
End Sub
End Class
I appreciate all of the answers here, but they are more complicated than I wanted to get in to.
I was very surprised to find out that there is a way to do it built into WScript.
WScript.Timeout = x_seconds
cscript
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
**//T:nn Time out in seconds: Maximum time a script is permitted to run**
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
Update:
To help people who downvote a plain (and to the point) citation of cscript.exe's usage message (how can that be wrong?) to see the light through #PanayotKarabakalov's smoke screen:
The claim:
using //T switch not guarantee real time accuracy
that all 5 Echo command executed, even if the Sleep time between them
is 1.5 second and the //T is set to 4
The evidence:
The script is restarted via:
CreateObject("WScript.Shell").Run "WScript " & _
Chr(34) & WScript.ScriptFullName & _
Chr(34) & " /T:4", 0, False
which does not contain the host-specific //T (as opposed to the script-specific /T) switch.
The (counter) argument:
Whatever way you start the first instance of the script (//T or no //T), the second/relaunched instance will never have a time out and will always run to the bitter end.
If you still have doubts, change the invocation in P.'s script to
CreateObject("WScript.Shell").Run "WScript //T:4 " & _
and try it out.

Try-Catch-End Try in VBScript doesn't seem to work

I'm trying the following code:
Try ' DOESN'T WORK
Throw 2 ' How do I throw an exception?
Catch ex
'What do I do here?
End Try
but I'm getting the error Statement expected in the catch clause.
Does anyone know how I can catch/throw exceptions in VBScript using try/catch? (I am not looking for solutions with On Error Do X.)
Handling Errors
A sort of an "older style" of error handling is available to us in VBScript, that does make use of On Error Resume Next. First we enable that (often at the top of a file; but you may use it in place of the first Err.Clear below for their combined effect), then before running our possibly-error-generating code, clear any errors that have already occurred, run the possibly-error-generating code, and then explicitly check for errors:
On Error Resume Next
' ...
' Other Code Here (that may have raised an Error)
' ...
Err.Clear ' Clear any possible Error that previous code raised
Set myObj = CreateObject("SomeKindOfClassThatDoesNotExist")
If Err.Number <> 0 Then
WScript.Echo "Error: " & Err.Number
WScript.Echo "Error (Hex): " & Hex(Err.Number)
WScript.Echo "Source: " & Err.Source
WScript.Echo "Description: " & Err.Description
Err.Clear ' Clear the Error
End If
On Error Goto 0 ' Don't resume on Error
WScript.Echo "This text will always print."
Above, we're just printing out the error if it occurred. If the error was fatal to the script, you could replace the second Err.clear with WScript.Quit(Err.Number).
Also note the On Error Goto 0 which turns off resuming execution at the next statement when an error occurs.
If you want to test behavior for when the Set succeeds, go ahead and comment that line out, or create an object that will succeed, such as vbscript.regexp.
The On Error directive only affects the current running scope (current Sub or Function) and does not affect calling or called scopes.
Raising Errors
If you want to check some sort of state and then raise an error to be handled by code that calls your function, you would use Err.Raise. Err.Raise takes up to five arguments, Number, Source, Description, HelpFile, and HelpContext. Using help files and contexts is beyond the scope of this text. Number is an error number you choose, Source is the name of your application/class/object/property that is raising the error, and Description is a short description of the error that occurred.
If MyValue <> 42 Then
Err.Raise(42, "HitchhikerMatrix", "There is no spoon!")
End If
You could then handle the raised error as discussed above.
Change Log
Edit #1:
Added an Err.Clear before the possibly error causing line to clear any previous errors that may have been ignored.
Edit #2:
Clarified.
Edit #3:
Added comments in code block. Clarified that there was expected to be more code between On Error Resume Next and Err.Clear. Fixed some grammar to be less awkward. Added info on Err.Raise. Formatting.
VBScript doesn't have Try/Catch. (VBScript language reference. If it had Try, it would be listed in the Statements section.)
On Error Resume Next is the only error handling in VBScript. Sorry. If you want try/catch, JScript is an option. It's supported everywhere that VBScript is and has the same capabilities.
Try Catch exists via workaround in VBScript:
http://web.archive.org/web/20140221063207/http://my.opera.com/Lee_Harvey/blog/2007/04/21/try-catch-finally-in-vbscript-sure
Class CFunc1
Private Sub Class_Initialize
WScript.Echo "Starting"
Dim i : i = 65535 ^ 65535
MsgBox "Should not see this"
End Sub
Private Sub CatchErr
If Err.Number = 0 Then Exit Sub
Select Case Err.Number
Case 6 WScript.Echo "Overflow handled!"
Case Else WScript.Echo "Unhandled error " & Err.Number & " occurred."
End Select
Err.Clear
End Sub
Private Sub Class_Terminate
CatchErr
WScript.Echo "Exiting"
End Sub
End Class
Dim Func1 : Set Func1 = New CFunc1 : Set Func1 = Nothing
Sometimes, especially when you work with VB, you can miss obvious solutions. Like I was doing last 2 days.
the code, which generates error needs to be moved to a separate function. And in the beginning of the function you write On Error Resume Next. This is how an error can be "swallowed", without swallowing any other errors. Dividing code into small separate functions also improves readability, refactoring & makes it easier to add some new functionality.

Resources