Guest Post by M Veldkamp
Last time I talked about iLogic and Forms. This time I want to discuss errors and error handling.
Like I’ve mentioned before, I made a rule that grew and grew and grew. Right now it’s over 1900 lines and it’s getting hard to track which user makes which errors sometimes.
It would be really neat if users emailed me about it. Most people don’t or if they do, they won’t include details like what settings they are using, on which iLogic rule the error occurred or even which assembly or drawing they tried it on.
Luckily you can do some real neat stuff in iLogic and VBA.
I found a forum post a couple of weeks ago where someone made it so you could send an email from iLogic through Outlook.
We use Outlook at work so I saw an opportunity! There are some things that I found out that I’d like to share with you today.
So, you can indeed use VBA (in iLogic) to send an email. It’s not even that hard!
You can call outlook with these lines right here
oOApp = CreateObject("Outlook.Application") oOMail = oOApp.CreateItem(0)
Now you can compose your email by accessing oOMail.
With oOMail .bodyformat = 2 .To = AdminEmailAdres .CC = "" .BCC = "" .Subject = "iLogic #ErrorMessage for " & iLogicName & "- Version:" & RuleVersion .Display End With Strbody = “Message." With oOMail .HTMLbody = Strbody & vbNewLine & .HTMLBody ' add the signature without losing the HTML-formatting of the signature '.Attachments.Add (FilePath & "\" & Picture & ".jpg") ‘if you get the thumbnail and put it in an object you can even send that '.Display ‘Toggle this on to show the user that an email is being sent. .send End With
“So how do you catch errors then”? I hear you asking.
Let’s go through the Sub Main() together.
Public Main Sub() On Error GoTo ErrorHandler ErrorMessage = “Get Filename” Dim Filename As String = ThisDoc.FileName 'This Is fine! ErrorMessage = “Trying to put a String in an Integer” Dim ThisWillError As Integer = ThisDoc.FileName 'This will be Not reached since there will be an Error In the Line above Exit Sub ErrorHandler: EmailOnError(ErrorMessage) End Sub
The “On error Goto ErrorHandler” is the most important here. Here is some documentation for those who want to know more.
It tells the rule to not freak out and go to ErrorHandler when something breaks. Now we just need to track what happens in our rule and we can start sending some emails!
In Main() we’re making markers. Every time we’re doing something that might potentially break we are going to change ErrorMessage. That way when the onError() is called, the last valid ErrorMessage can be used as a marker!
Last thing in Main() is the Exit Sub line just before the ErrorMessage. We don’t want to send an email when there’s no error so we’re exiting the sub before that happens. The On Error Goto ErrorHandler conveniently skips that Exit Sub line so we can do what we wish to do.
Now… there’s another thing I learned whilst talking to friends. One friend asked me if any of the users that use the tool are from the EU and if I knew the ins and outs of the GDPR laws.
Now, after my minimal research, I concluded that I wouldn’t need to inform the users since they came from the US. That might not be the case in your tool or rule!
So what do we need to do and how do we need to do it?
In short. We need to ask for consent from the users so we can email something without the users typing and sending the email.
To keep these settings private we simply create a text file on the users’ C drive which tells the rule if the users is OK with sending emails or not.
NOTE: You don’t need to do this. You can bypass this whole settings thing by just asking the users EVERY time if it’s okay.
Maybe you’ve already seen an error in an iLogic code. Most of them look something like this.
Now for the code! Try this out for yourself and see what happens. Note: You need Outlook installed and in use, you also need to have the folder TEMP on C:/ so the rule may place the EULA.txt file there.
'Original code by M Veldkamp 'Posted at https://clintbrown.co.uk/GDPR - Always check local law regarding email Class iLogicRule Shared iLogicName As String = "iLogicRule" Shared RuleVersion As Integer = 1 Shared ErrorMessage As String Shared AdminEmailAdres As String = "email@example.com" Public Sub Main() On Error GoTo ErrorHandler ErrorMessage = “Get Filename” Dim Filename As String = ThisDoc.Filename ErrorMessage = “Trying to put a String in an Integer” Dim ThisWillError As Integer = ThisDoc.Filename 'This will be Not reached Exit Sub ErrorHandler : EmailOnError(ErrorMessage) End Sub Sub EmailOnError(ErrorMessage As String) If TermsAndConditions = False Then Exit Sub 'In the EU you NEED consent From the users. If EmailSent > 5 Then Exit Sub 'Limit the rule so it can only send a max Of 5 emails per run. Try oOApp = CreateObject("Outlook.Application") oOMail = oOApp.CreateItem(0) Catch MessageBox.Show("Error Message Not Sent. Please email the issue to” & AdminEmailAdres, "Title") Exit Sub End Try strbody = _ "<p style='font-family:calibri;font-size:16'>" & _ "This is an automated ErrorMessage:" & "<br><br>" & _ "The error is thrown in the " & iLogicName & "<br><br>" & _ "Error by: " & ThisApplication.UserName & "<br>" & _ "Error occured in " & ThisDoc.FileName(True) & "<br>" & _ "ErrorMessage for today: " & "<br>" & _ ErrorMessage & "<br><br>" & _ "</P>" With oOMail .bodyformat = 2 .To = AdminEmailAdres .CC = "" .BCC = "" .Subject = "iLogic #ErrorMessage for " & iLogicName & "- Version:" & RuleVersion .Display End With With oOMail .HTMLbody = strbody & vbNewLine & .HTMLBody ' add the signature without losing the HTML-formatting of the signature '.Attachments.Add (FilePath & "\" & Picture & ".jpg") .Display 'Toggle this on to show the user that an email is being sent. .send End With EmailSent = EmailSent + 1 End Sub Function TermsAndConditions() '''Ask the user For permission To email something In the background.You NEED consent For this For ALL users In the EU. '''Returns True If consent Is given. '''Returns False If consent Is Not given '''Asks For consent If no EULA File has been found. EULAFile = "C:\TEMP\iLogicRule EULA.txt" Dim oFileExists As Boolean = System.IO.File.Exists(EULAFile) If oFileExists = True ' EULA File already Exists. Question has already been asked. oRead = System.IO.File.OpenText(EULAFile) Dim EULAContent As String = oRead.ReadToEnd() 'Check If the File contains “Yes” If InStr(1, EULAContent, "Yes", vbBinaryCompare) Return True Else Return False End If Else' If oFileExists = False 'EULA File doesn't exist yet. Ask the user for consent. oWrite = System.IO.File.CreateText(EULAFile) ConsentForm = vbNewLine _ & "Terms and Conditions:" & vbNewLine _ & "Do you consent to auto-send an email in the case of an error?" & vbNewLine _ & "This will help to fix the error. No user interaction is required. " & vbNewLine _ & "This message will only show once. " & vbNewLine AnwserFromUser = MessageBox.Show(ConsentForm, iLogicName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) If AnwserFromUser = vbYes oWrite.WriteLine("Yes") oWrite.Close() Return True Else oWrite.WriteLine("No") oWrite.Close() Return False End If End If End Function End Class
A copy of the iLogic code in a text file can be found here (just in case there are any issues copying and pasting this large chunk of code). California is currently working on passing similar laws to the European GDPR, and other states may follow, so please check local laws before sending loads of emails.
Here is a screenshot of the email. Remember to change the email address in the code!
Please note that I cannot offer any additional support, this blog post is offered as-is, and is aimed at the more advanced user.
As always, please test all iLogic code extensively on non-production files. Do not use any code in a production environment until YOU have thoroughly tested it and have verified that it works as expected. Always back up any data before running any experimental code. You are ultimately responsible for any iLogic code that you run, so make sure you test it thoroughly!