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]
Parents
  • 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.

Reply
  • 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.

Children
No Data