问题描述
我对VBS不太熟悉,不确定如何最好地解决这个问题。
我已经使用了这个基本代码,当来自机器的值> = 100时,它会发送一封电子邮件。每当变量值更改时,WinCC都会触发该脚本。
现在,我想在许多其他值上利用此值来监视机械设备的零件并发送一些电子邮件警报。
但是,是否有必要在每个脚本中复制整个电子邮件设置代码,还是有一种方法可以使触发的代码在其中包含电子邮件设置的情况下调用全局脚本?
因此,而不是“已触发的VBS-检查值-如果为True-这是电子邮件详细信息-发送电子邮件”
它更像是“已触发的VBS-检查值-如果为True-加载电子邮件设置VBS-发送电子邮件”
希望有道理吗?
Option Explicit
Function action
Dim TagVari1
Dim TagVari2
Set TagVari1 = HMIRuntime.Tags("TestTag1")
TagVari1.Read
TagVari1.Value = TagVari1.Value +1
Set TagVari2 = HMIRuntime.Tags("TestTag2")
TagVari2.Read
TagVari2.Value = TagVari1.Value
TagVari2.Write
If TagVari2.Value >= 100 Then
Dim objMessage
Set objMessage = CreateObject("cdo.message")
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mydomain.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "my.email@mydomain.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = "30"
objMessage.Configuration.Fields.Update
objMessage.Subject = "WinCC Message"
objMessage.From = "my.email@mydomain.com"
objMessage.To = "recip.email@outlook.com"
objMessage.TextBody = "This is a test message from WinCC......"
objMessage.Send
x=MsgBox("CHP Alarm","Tag2 equal or over 100")
End If
End Function
解决方法
这是如何添加另一个* .vbs的方法,这要感谢Frank-Peter Schultze
将此Sub放入您的主脚本中:
'------------------------------------------------------------------------------
'Purpose : Include another VBScript file into the current one.
'Note : Usage: Include("vbsfile.vbs")
'
' Author: Frank-Peter Schultze
' Source: http://www.fpschultze.de/smartfaq+faq.faqid+51.htm
'------------------------------------------------------------------------------
Sub Include(ByVal strFilename)
Dim objFileSys,objFile,strContent
Set objFileSys = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSys.OpenTextFile(strFilename,1)
strContent = objFile.ReadAll
objFile.Close
Set objFileSys = Nothing
ExecuteGlobal strContent
End Sub
'------------------------------------------------------------------------------
让电子邮件发送例程使用其他脚本,例如MySendMail.vbs
然后在主脚本开始的某个地方调用它
Include("Full\Path\To\MySendMail.vbs")
这是一个警告:必须将包含的文件名及其完整路径(包括驱动器)传递给Sub。