0

I'm not overly familiar with VBS and not sure how to best approach this.

I've got this basic code to work whereby when a value from a machine >= 100 it send out an email. The WinCC triggers this script whenever the tag value changes.

Now, I want to utilise this on a number of other values to monitor parts of machinery and equipment and send out some email alerts.

But, is there any need to replicate the whole email settings code in every script or is there a way that the triggered code can call a global script with the email settings in?

So instead of "Triggered VBS - Check Value - If True - Here's email details - Send Email"

Its more like "Triggered VBS - Check Value - If True - Load Email Setting VBS - Send Email"

Hope that makes sense?

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
user692942
  • 16,398
  • 7
  • 76
  • 175
Mark Lee
  • 13
  • 5
  • Does it have to be in another vbs? Can't you just create a sub or function in the same file and call that? – Geert Bellekens Nov 05 '20 at 14:51
  • 1
    Does this answer your question? [How do I include a common file in VBScript (similar to C #include)?](https://stackoverflow.com/questions/316166/how-do-i-include-a-common-file-in-vbscript-similar-to-c-include) – user692942 Nov 05 '20 at 19:59
  • Geert, no, that wouldn’t work. In WinCC there are actions that triggers the script. So there maybe multiple actions depending on what’s being monitored and each one triggers a different script. When it comes to emailing alerts, I may have a bunch of different actions all using the same script within it. I’d like to each script write the variables and then run a separate email script. That way, if I need to change email details in future, there’s only one piece of script to modify not 30 or 40 scripts. – Mark Lee Nov 06 '20 at 06:55
  • @MarkLee so the duplicate doesn’t help? I’m afraid they are your only options and if none of them work for WinCC your out of luck, but seen as though you didn’t indicate whether you’ve even look at them or not I guess we won’t know. – user692942 Nov 06 '20 at 08:34
  • Lanymart - yes having it duplicated works fine. I have 3 different scripts setup for testing and each one contains the same email Code but with different subjects, bodies, emails, etc. I want to roll this out for many more alerts, currently I have 37 identified. But before I do, I want to know if I can get away from duplicate code so I have one point of email confit code. If I ever have to change it, I’ll have 37+ pieces of code to change. – Mark Lee Nov 06 '20 at 10:13
  • @MarkLee I wasn't referring to duplicating code, I was referring to [the duplicate question](https://stackoverflow.com/questions/64698739/can-i-eliminate-vbs-duplicated-code-via-a-global-script#comment114405887_64698739) posted in the comments and whether that helped. – user692942 Nov 06 '20 at 12:24

1 Answers1

0

Here's how to include another *.vbs, courtesy of Frank-Peter Schultze

Put this Sub in your main script(s):

'------------------------------------------------------------------------------
'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
'------------------------------------------------------------------------------

Have your email send routine in another script, e.g. MySendMail.vbs

And then somewhere at the start of your main script call it like

Include("Full\Path\To\MySendMail.vbs")

And that's the one caveat: the included filename must be passed to the Sub with its full path including drive.

Hel O'Ween
  • 1,423
  • 9
  • 15