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]