I have noticed that the following is repeated in my coding often. Is there a way to make it into 1 instance that when needed the code could reference it and run?
Such as this with the PDF output:
Code:
With objOutlookMsg
.Subject = NIE & " Closed Changes - " & Tod
.Body = SigBlock
DoCmd.OutputTo 3, "rptClosedCRStats", acFormatPDF, "C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf", , 0
.Attachments.Add ("C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf")
.Display
Kill "C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf"
Example:
.Subject = NIE & " Closed Changes - " & Tod
NIE = Exersize
TOD - Todays date formatted
Closed changes references the form
Could this code be put into 1 "module" for all others to reference? Like Call form_CodeEmail_Click? Then also where "Closed changes" be a Variable/String?
make a table listing all the report & query names and in the next column put what the variable/string would be named? Then reference Column 2 for the report name and Column 3 for the "Closed change" part of the subject line?
IE:
Code:
With objOutlookMsg
.Subject = NIE & OutputName & Tod
.Body = SigBlock
DoCmd.OutputTo 3, "RPTName", acFormatPDF, "C:\Temp\" & NIE & OutputName & Tod & ".pdf", , 0
.Attachments.Add ("C:\Temp\" & NIE & OutputName & Tod & ".pdf")
.Display
Kill "C:\Temp\" & NIE & OutputName & Tod & ".pdf"
OR
Code:
ErrorMsgs:
If Err.Number = "287" Then
MsgBox "You clicked No to the Outlook security warning. " & _
"Rerun the procedure and click Yes to access e-mail " & _
"addresses to send your message."
Else
MsgBox Err.Number & " " & Err.Description
End If
Where I can call this Error code as a module like Tod?
Would you use something like:
Public Function ErrorSub
...........
Unsure of how this goes
Probably it would be the same for the DIMs and Sets
Thanks
Code:
Public Sub CMD_EMail_Min_Click()
On Error GoTo ErrorMsgs
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.Subject = NIE & " Closed Changes - " & Tod
.Body = SigBlock
DoCmd.OutputTo 3, "rptClosedCRStats", acFormatPDF, "C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf", , 0
.Attachments.Add ("C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf")
.Display
Kill "C:\Temp\" & NIE & " Closed Changes - " & Tod & ".pdf"
End With
DoCmd.Close acReport, "rptClosedCRStats"
DoCmd.OpenForm "frmComplete"
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookAttach = Nothing
Exit Sub
ErrorMsgs:
If Err.Number = "287" Then
MsgBox "You clicked No to the Outlook security warning. " & _
"Rerun the procedure and click Yes to access e-mail " & _
"addresses to send your message."
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Public Sub EMailClosedSoftware_Click()
On Error GoTo ErrorMsgs
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.Subject = NIE & " Closed Software - " & Tod
.Body = SigBlock
DoCmd.OutputTo 3, "rptClosedSW", acFormatPDF, "C:\Temp\" & NIE & " Closed Software - " & Tod & ".pdf", , 0
.Attachments.Add ("C:\Temp\" & NIE & " Closed Software - " & Tod & ".pdf")
.Display
Kill "C:\Temp\" & NIE & " Closed Software - " & Tod & ".pdf"
End With
DoCmd.Close acReport, "rptClosedSW"
DoCmd.OpenForm "frmComplete"
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookAttach = Nothing
Exit Sub
ErrorMsgs:
If Err.Number = "287" Then
MsgBox "You clicked No to the Outlook security warning. " & _
"Rerun the procedure and click Yes to access e-mail " & _
"addresses to send your message."
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub