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.

  • 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

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

Children
  • What do you mean by if the same program is grabbed from your server a second time? Do you mean used by another machine at the same time? Or closed, then reopened, and ran again? Because instance 1 is a no-no

  • I create the programs and they are saved onto a server. The operators grab the program and put it into a folder on their desktop. They run the program from that folder. If they have the same part to run afterwards, i dont allow them to just hit play again, they have to delete the data on their desktop, grab the program again, then start.

  • I would start by simplifying your code to its minimal form required to send e-mails.  Remove the variables and objects that are not directly related to sending e-mail.  Prove that this works in all cases before expanding on it.  Here is an example I used offline -

    Sub Main()

    Dim oApp As Object
    On Error Resume Next
    Set oApp = CreateObject("Outlook.Application")
    On Error GoTo 0

    If IsEmpty(oApp) Then
    ' Outlook is Not running, create a new instance
    Set oApp = CreateObject("Outlook.Application")
    Else
    ' Outlook is already running, use the existing instance
    Set oApp = GetObject("","Outlook.Application")
    End If

    Dim oMail As Object

    Set oMail = oApp.CreateItem(olMailItem)
    oMail.Body = "Body of the email"
    oMail.Subject = "Test Subject"
    oMail.To = "name@domain.com"  ' ****** put your e-mail address here
    oMail.Send

    Set oMail = Nothing
    Set oApp = Nothing

    End Sub

    One thing I did see with my own test, if I am running PC-DMIS As Administrator, this works if Outlook is closed because it runs Outlook (in the background) s administrator.  If Outlook is already open, it needs to have been opened as administrator.  

    If PC-DMIS is NOT run As administrator, then this works offline whether outlook is open or closed.

    My point is, let's start with something smaller and work our way up.

    In reality, this code above can be reduced even further, to something like this -

    Sub Main()

    Dim oApp As Object

    Set oApp = CreateObject("Outlook.Application")

    Dim oMail As Object

    Set oMail = oApp.CreateItem(olMailItem)
    oMail.Body = "Body of the email"
    oMail.Subject = "Test Subject"
    oMail.To = "dlruggieri@yahoo.com"
    oMail.Send

    Set oMail = Nothing
    Set oApp = Nothing

    End Sub

    which works whether Outlook is open or not.  The main points are this

    Both Outlook and PC-DMIS need to be run at the same level, User or Administrator
    Reduce the code to the smallest possible form that works and go up from there

  • How would you modify this if you're using a web based version of Outlook?

  • That would take a lot of extra steps to get there, and I think may require calls to an external API of some sort for email handling. The outlook application is much simpler because it has automation methods built into it. You may need to use the Azure services Microsoft offers, or enable SMTP protocols through your email account. 

    https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040

  • I've done this with Yahoo if it is of any value to you