From a previous blog post (reference blog post) we discussed automating Minitab to generate reports on the capability of our process. Today I would like to use the same methods for automating the analysis, but instead of generating a report, we will send an email when the control chart fails.
I will start by using the same Minitab executable file (blog.mtb) that was covered in my previous posts, and then show how to extend the functionality with C#.
Prerequisites
- You will need a working version of the blog.mtb file (covered here)
- You will need to have access to Visual Studio.
- Programming experience would be helpful to customize the code to fit your scenario, but I will provide comments along the way.
Code in C# to Test for Out of Control Points
//Declare Minitab Variables (Reference Mtb 17.0 Type Library in Visual Studio)
Mtb.Application mtbApp = null;
Mtb.Project mtbProj = null;
Mtb.Worksheet mtbSheet = null;
Mtb.UserInterface mtbUI = null;
int oocPoints;
//Create Minitab Instance
mtbApp = new Mtb.Application();
mtbProj = mtbApp.ActiveProject;
mtbSheet = mtbProj.ActiveWorksheet;
mtbUI = mtbApp.UserInterface;
mtbUI.Visible = false;
mtbUI.DisplayAlerts = false;
//Execute Minitab Command Language
mtbProj.ExecuteCommand(@"Execute 'C:\Users\dgriffith\Desktop\blog.mtb' 1.");
//Find out if points are out of control
oocPoints = (int)mtbSheet.Constants.Item(1).GetData();
Code in C# to Send Email if Chart is Out of Control
//Declare directive for Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
// If out of control points are more than 0 then send email
if (oocPoints>0)
{
//If out of control, save control chart and send email
mtbProj.Commands.Item(4).Outputs.Item(1).Graph.SaveAs(
@"C:\Users\dgriffith\Desktop\controlchart.JPG", true,
Mtb.MtbGraphFileTypes.GFJPEG);
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem eMail =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
eMail.To = "mentoring@minitab.com";
eMail.Subject = "Control Chart Failed";
eMail.Body = "Attached is the Control Chart that failed";
eMail.Attachments.Add(@"C:\Users\dgriffith\Desktop\controlchart.JPG");
((Outlook._MailItem)eMail).Send();
}
Scheduling a Task to Run Daily
After adding the C# code, you will want to build the Solution and ensure that the program runs correctly. If everything works as desired, you can set up a scheduled task to automatically run your program using the steps below:
1. In Windows, go to the Control Panel > Administrative Tools > Task Scheduler.
2. Choose the Option to Create Basic Task.
3. Name and provide a Description for the program we built; click Next.
4. Check the option Daily; click Next.
5. Specify the date and time of day you would like the program to run.
6. Check the option for Start a Program; click Next.
7. Browse to the .exe file that was created when building the program; click Next.
8. Click Finish.
Congratulations! If you have successfully automated the sending of an email when a control chart is out of control in addition to automatically creating Monthly Reports, we are getting closer to putting you out of a job. Let's see what we can do next!
If you have questions, feel free to send me an email at mentoring@minitab.com. If you are looking for a customized solution, we are always glad to help.