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

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

  • So I did try this, and removed the unused variables. Still not getting it to keep from crashing. My updated code is in a comment below.