VBS脚本尝试通过电子邮件发送错误

时间:2015-06-25 16:31:32

标签: vbscript

我有问题VBS试图通过电子邮件发送错误。这是错误 第1行 Char 13 错误下标超出范围 代码800A0009

 Dim param1: param1 = WScript.Arguments(0)
EmailTo = "name@email.com"
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

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

脚本的简化版

Option Explicit

Dim param1 : param1 = WScript.Arguments(0)
WScript.Echo param1
如果没有参数调用

将会中断:

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

但按预期使用时“有效”:

cscript 31055894.vbs parameter
parameter

改进版本:

Option Explicit

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

将处理这两种用法:

cscript 31055894.vbs
default

cscript 31055894.vbs pipapo dontcare
pipapo

答案 1 :(得分:1)

Arguments属性(适用于WScript对象)包含WshArguments对象(参数集合)。使用从零开始的索引从此集合中检索单个参数。

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