hexagon logo

Help with auto email code

Hey there, 

I'm not all that familiar with writing code but have been messing with it a bit more lately.

I started a code to send an email to myself if a report has a feature that is OOT. The reason for this isn't critical, i just don't get to be out on the floor a lot and don't normally get a lot of feedback from operators so i thought it would be nice to check my emails in the mornings and see if we had a lot of issues the previous night.

With this code it only works half of the time, and the other half it crashes the software before the email is sent. Could anyone look through and see what i maybe doing wrong? 

I essentially have the program autosave a .PDF and rename it using the work order and operation of inspection input by the operator at the beginning of the program. The email will grab that .PDF and then email it to me. The .PDF and the program autosaves just fine, that's why i think the problem lies within the email code.

Sub Main()

Dim strbody As String
Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "AUTO EMAIL SENT FOR OOT CONDITION"

    On Error Resume Next
    With OutMail
Dim pcapp As Object
Dim pcpart As Object
Dim App As Object
Dim Part As Object
Dim pdf_report_path As String
Dim source_path As String
Dim Serial As Object
Dim RERUN1 As Object
Dim SFC2 As Object
Dim OP As Object
Dim Pathname As Object
Dim fso As Object
Dim strPath As String
    Set App = CreateObject ("PCDLRN.Application")
    Set Part = App.ActivePartProgram
    Set Serial = Part.GetVariableValue ("PARTNUM")
    Set SFC2 = Part.GetVariableValue ("SFC2")
    Set OP = Part.GetVariableValue ("OP")
    Set RERUN1 = Part.GetVariableValue ("RERUN1")
    Set Pathname = Part.GetVariableValue ("JOB_PATH5")
        strPath = Part.Path

     source_path = strPath+Serial.StringValue & "_" & SFC2.StringValue & "_" & OP.StringValue & RERUN1.StringValue & ".PDF"

        .To = "*EMAIL ADDRESS WAS HERE*"
        .CC = ""
        .BCC = ""
        .Subject = "AUTO REPORT EMAIL"
        .BodyFormat = 1
        .Body = strbody
        .Attachments.Add source_path
        .Send

    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub



Edit** When i run the code offline it works perfect every time. The crashes only happen when connected to a machine and running.
[edited by: Jamison at 3:11 PM (GMT -6) on Dec 19, 2024]
  • I see quite a few variables that are not being used. I believe they are just duplicates. 

    pcapp, pcpart, pdf_report_path, fso.

  • Would duplicates or unused variables cause the software to crash? or would it just be clean up? I can totally take those out, just didn't think they were causing issues as i just copied and pasted from my auto save .BAS

  • Not trying to change your notification method but have you considered exporting your measurement data into a .csv or excel? Then you can review your data every morning without having to seek any feedback.

    I am interested in this email method though. I have some critical parts that would be nice to get an email as soon as a part failed with the attached report.

  • I do have the reports saved automatically and propagated to a special directory for SPC tracking. We then have another software called CM4D that tracks each feature. That code works just fine. However the email notification i wanted to add for special programs that are new so i can be more involved right away if issues arise. Again its not critical to our production, but i thought it would be a cool process improvement.

  • Does the program/s have all the PC-DMIS variables your code is looking for?

    source_path = strPath+Serial.StringValue & "_" & SFC2.StringValue & "_" & OP.StringValue & RERUN1.StringValue & ".PDF"

    Are there StringValues present for all these variables?

    Is it one login or several logins (per operator)? Do they all have a mail program installed?

    Sub Main()
    Dim pcapp As Object
    Dim pcpart As Object
    Dim App As Object
    Dim Part As Object
    Dim pdf_report_path As String
    Dim source_path As String
    Dim Serial As Object
    Dim RERUN1 As Object
    Dim SFC2 As Object
    Dim OP As Object
    Dim Pathname As Object
    Dim fso As Object
    Dim strPath As String
        Set App = CreateObject ("PCDLRN.Application")
        Set Part = App.ActivePartProgram
        Set Serial = Part.GetVariableValue ("PARTNUM")
        Set SFC2 = Part.GetVariableValue ("SFC2")
        Set OP = Part.GetVariableValue ("OP")
        Set RERUN1 = Part.GetVariableValue ("RERUN1")
        Set Pathname = Part.GetVariableValue ("JOB_PATH5")
        strPath = Part.Path
    
        source_path = strPath+Serial.StringValue & "_" & SFC2.StringValue & "_" & OP.StringValue & RERUN1.StringValue & ".PDF"
    
    Dim strbody As String
    Dim OutApp As Object
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        strbody = "AUTO EMAIL SENT FOR OOT CONDITION"
    
        On Error Resume Next
        With OutMail
    
            .To = "*EMAIL ADDRESS WAS HERE*"
            .CC = ""
            .BCC = ""
            .Subject = "AUTO REPORT EMAIL"
            .BodyFormat = 1
            .Body = strbody
            .Attachments.Add source_path
            .Send
    
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

    I would have moved all the PC-DMIS thingies outside the "With OutMail" block.

  • Yeah it does have all of the variables before the .BAS in the program. It will work sometimes, but other times fail. And it between multiple operators. They all have their own pass worded log in to the CMM computer, and they all do have the mail program installed. I did also verify that if their mail application wasn't opened at all that it would still send an email. But it doesn't seem to be picky on a certain person or instance, seems to be pretty random.

    The strPath should pull the location on their desktop or in a folder on their desktop. 

    Then the Serial.StringValue & "_" & SFC2.StringValue & "_" & OP.StringValue & RERUN1.StringValue & ".PDF" will pull the .PDF that is saved and renamed using those same variables.

  • Okay i can give that a shot and see if that makes a difference.

  • The only reason I mentioned them is because if any of them are unused, but important to your code But since this works offline it probably isn't the case. Just trying to root out unused variables in case they are actually needed, and I'm overlooking something

    Like I see Pathname is linked to your "JOB_PATH5" variable. Is that also not needed? Probably not because of the Part.Path.

    Does the crash occur upon completion of the program when it should send the email during online use?

  • Yeah i originally was using the  "JOB_PATH5", but changed it to the part.Path because it wasn't working. I think i could remove that a well. I have an auto save .BAS right before the email .BAS. What the auto save does, is automatically saves the program, and the .PDF in the folder they are running the program on. It then copies the .PRG and the .PDF over to another folder on our servers. It successfully saves and copies both files over to the server. It then immediately goes to the email .BAS, in which the software will crash, and the email wont send. A couple of times a day it will send the email just fine. In which it doesn't crash if the email sends.

  • Okay, I've updated the code and used this. Still cant get it to be consistent. It will send the email in some instances, then if the same program is grabbed from our server a second time and ran it crashes. 

    Sub Main()
    Dim App As Object
    Dim Part As Object
    Dim source_path As String
    Dim Serial As Object
    Dim RERUN1 As Object
    Dim SFC2 As Object
    Dim OP As Object
    Dim strPath As String
        Set App = CreateObject ("PCDLRN.Application")
        Set Part = App.ActivePartProgram
        Set Serial = Part.GetVariableValue ("PARTNUM")
        Set SFC2 = Part.GetVariableValue ("SFC2")
        Set OP = Part.GetVariableValue ("OP")
        Set RERUN1 = Part.GetVariableValue ("RERUN1")
        strPath = Part.Path
    
        source_path = strPath+Serial.StringValue & "_" & SFC2.StringValue & "_" & OP.StringValue & RERUN1.StringValue & ".PDF"
    
    Dim strbody As String
    Dim OutApp As Object
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        strbody = "AUTO EMAIL SENT FOR OOT CONDITION"
    
        On Error Resume Next
        With OutMail
    
            .To = "*MY EMAIL*"
            .CC = ""
            .BCC = ""
            .Subject = "AUTO REPORT EMAIL"
            .BodyFormat = 1
            .Body = strbody
            .Attachments.Add source_path
            .Send
    
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub