1
votes

Im having problem VBS trying to email errors. here is the error Line 1 Char 13 Error Subscript out of Range Code 800A0009

 Dim param1: param1 = WScript.Arguments(0)
EmailTo = "[email protected]"
Set oWshNetwork = WScript.CreateObject("WScript.Network")
ComputerName = oWshNetwork.ComputerName
Set objEmail = CreateObject("CDO.Message")
objEmail.From = ComputerName & "@email.com"
objEmail.To = EmailTo
objEmail.Subject = "TESTING"
objEmail.Textbody = objEmail.Textbody & vbCrLf & "Please see attached log file for batches 

unzipped yesterday."
objEmail.Configuration.Fields.Item

("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item

("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mail.com"
objEmail.Configuration.Fields.Item

("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.AddAttachment param1
objEmail.Send

Thanks for all your help!

2

2 Answers

1
votes

This simplified version of your script

Option Explicit

Dim param1 : param1 = WScript.Arguments(0)
WScript.Echo param1

will break if called without a parameter:

cscript 31055894.vbs
... 31055894.vbs(3, 14) Microsoft VBScript runtime error: Subscript out of range

but 'works' when used as intended:

cscript 31055894.vbs parameter
parameter

An improved version:

Option Explicit

Dim param1 : param1 = "default"
If 0 < WScript.Arguments.Count Then param1 = WScript.Arguments(0)
WScript.Echo param1

will handle both usages:

cscript 31055894.vbs
default

cscript 31055894.vbs pipapo dontcare
pipapo
1
votes

The Arguments property (applies to WScript object) contains the WshArguments object (a collection of arguments). Use a zero-based index to retrieve individual arguments from this collection.

Dim param1
If WScript.Arguments.Count > 0 Then
  param1 = WScript.Arguments(0)
Else
  param1 = ""
End If
'
'''
'
If CreateObject("Scripting.FileSystemObject").FileExists(param1) Then objEmail.AddAttachment param1
objEmail.Send