我对VBS不太熟悉,也不知道如何最好地解决这个问题。
我有这样的基本代码,当机器的值>=100它发出一封电子邮件。只要标签值发生变化,WinCC就会触发该脚本。
现在,我想将其用于其他一些值,以监控机械和设备的零件并发送一些电子邮件警报。
但是,是否需要在每个脚本中复制整个电子邮件设置代码,或者有没有一种方法可以让触发的代码调用中包含电子邮件设置的全局脚本?
因此;触发VBS-检查值-如果为True-下面是电子邮件详细信息-发送电子邮件";
它更像是";触发VBS-检查值-如果为真-加载电子邮件设置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" ,0, "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("FullPathToMySendMail.vbs")
这是一个警告:包含的文件名必须传递给Sub,其完整路径包括驱动器。