VBA Email notification not sending out when its a different case

Windowtothewall

New Member
Joined
Sep 7, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hey Guys,

I have inherited some code from an ex colleague that he didnt get to finish before he moved on, and I cant seem to get to work and would like some assistance on please.

The error I'm getting is:

Run Time error -2147467259 (80004005)

Outlook does not recognize one or more names



The code is for a Submit Form which sends emails out to selected addresses from a reference table if YES is displayed.

Problem is, it works only when selecting Severity = Critical. But not the other two options (Major and Minor)

When debugging it highlights the text in RED below

'Create array of email addresses
Dim checkrange As Range

Select Case cbSeverity.Value
Case "Critical"
Set checkrange = Range("C2", Range("C2").End(xlDown))
For Each cell In checkrange
If cell = "Yes" Then
Set objEmail = objOutlook.CreateItem(0)
With objEmail
.to = cell.Offset(0, -1).Value
.Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
.body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
& "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
& "Location: " & entLocation & vbNewLine _
& "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
& "Description: " & entDescription.Value & vbNewLine _
& "Severity: " & cbSeverity.Value & vbNewLine _
& "Priority: " & cbPriority.Value & vbNewLine _
& "Target Date: " & entTargetDate.Value
.Send
Set objEmail = Nothing
End With
End If
Next cell
Case "Major"
Set checkrange = Range("D2", Range("D2").End(xlDown))
For Each cell In checkrange
If cell = "Yes" Then
Set objEmail = objOutlook.CreateItem(0)
With objEmail
.to = cell.Offset(0, -1).Value
.Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
.body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
& "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
& "Location: " & entLocation & vbNewLine _
& "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
& "Description: " & entDescription.Value & vbNewLine _
& "Severity: " & cbSeverity.Value & vbNewLine _
& "Priority: " & cbPriority.Value & vbNewLine _
& "Target Date: " & entTargetDate.Value
.Send
Set objEmail = Nothing
End With
End If
Next cell
Case "Minor"
Set checkrange = Range("E2", Range("E2").End(xlDown))
For Each cell In checkrange
If cell = "Yes" Then
Set objEmail = objOutlook.CreateItem(0)
With objEmail
.to = cell.Offset(0, -1).Value
.Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
.body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
& "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
& "Location: " & entLocation & vbNewLine _
& "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
& "Description: " & entDescription.Value & vbNewLine _
& "Severity: " & cbSeverity.Value & vbNewLine _
& "Priority: " & cbPriority.Value & vbNewLine _
& "Target Date: " & entTargetDate.Value
.Send
Set objEmail = Nothing
End With
End If
Next cell
End Select
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
where is this variable declared AND what is it's value ?
VBA Code:
cbSeverity.Value
Also, can you please use code tags when posting code....Simply press the VBA button in the reply toolbar, then paste the code where the cursor is flashing between the tags.
ALSO.....Is that ALL of the code ??
Can you also post a SMALL sample of your data layout...using XL2BB, see my signature block for this.
 
Upvote 0
My apologies. If i have done this incorrectly still let me know.

Here is the where it is taking the data from.

VBA Code:
cbSeverity.Value
is taken from the Cells with Yes in them, ignore Pm overdue. ( separate function)

Email Notification Reference
NameEmailCritical EMRMajor EMRMinor EMRInitialPM Due in 3 DaysPM Overdue
MitchYesYesYesMMYesYes


Here is the data table

EMR Log v2.5.xlsm
ABCDEFGHIJKLMNOPQRSTU
10 09-Jun-21EquipmentNilfisk Floor ScrubberMitchEngineeringProductionNAVacuum suction not workingNAMinor210-Jun-2110-Jun-21MitchFuse blown and lose terminal. Clean vacuum motor, crimp terminal and replace with new fuse.$ 15.3415-Jun-21RCClosed
1121-00211-Jun-21EquipmentAPACRonaldEngineeringProductionNAPLC software update and Sauce Mix Hopper Nozzle DivisionNAMinor111-Jun-2111-Jun-21Zane, Mitch, RonaldNew software feature for sauce mix auger timer and vacuum conveyor. New "flower" divider at sauce mix hopper nozzle. Update PMB dosing speed from 2500 to 1200$ -15-Jun-21RCClosed
1221-00316-Jun-21EquipmentBoiler IgnitorMitchEngineeringWarehouseNAIgnitor Locking Out, reset 8 times to get it to fire.NACritical316-Jun-2116-Jun-21MitchABS have been contacted 16-6-21$ -24-Jun-21RCMitch reset it by pulling the plunger at gas line pressure relief valve.Closed
1321-00416-Jun-21EquipmentOxygen Absorber MachineProduction TeamEngineeringProduction2814:001Cutting at incorrect intervals. Faulting on end of film when film is not at end.NAMajor216-Jun-2116-Jun-21RonaldReadjusted the Green Film detection Sensor. Linear Slide worn.$ -16-Jun-21RCNew Linear Slide needed.Closed
1421-00524-Jun-21EquipmentRay75LeightonEngineeringWarehouseNAA9 error on Ammonia Shut off valve vapor trap side 1 H1G1Q3NAMajor215-Sep-21Sarah from TaitControl is tied up. New software schedule on early Sept.$ -Open
1521-00624-Jun-21EquipmentAPACRonaldEngineeringProductionNANew suction cups on st#1 bag pickup and st#1 zipper open. Readjust all 6 new suction cups positions. Add in filters for bag pickup suction cups. Install vacuum conveyor. Clean heat sealer bar and replace the teflon tape. Load new software for vacuum conveyorNAMajor230-Jun-2130-Jun-21RonaldN/A$ -31-Jul-21MMN/AClosed
1621-00724-Jun-21EquipmentAmmonia CompressorMitchEngineeringPlant RoomNAAmmonia leaks at air compressor 2, triggered alarm.NAMajor125-Jun-2125-Jun-21CIR - ContractorNotified service company to repair the leaks. Leak location found and 4 o-ring replaced.$ -25-Jun-21RCClosed
1721-00820-Jul-21EquipmentAPACMitchEngineeringProductionnaSuction cup broken, gripper missing. Bags sealing on angleNACritical121/7/202120-Jul-21Replaced gripper and glued in. Cam 17 350-100 changed to 350-180, grippers cleaned, Infeed conveyor gap decreased$ -20-Jul-21MMLost Gripper found by Pita on top of Nuripack base.Closed
1821-00922-Jul-21EquipmentAPAC GrippersPitaProductionAPACNAGripper 2 & 10 often dislodge and need to be put back in place.Critical227/07/2021Open
1921-01007-Sep-21EquipmentWork BenchMichaelProductionTray LoadingWelding has deteriorated on weighing platformCritical209-Oct-21Open
EMR_Log
Cell Formulas
RangeFormula
U10:U19U10=IF(ISBLANK([@[Target Date]]),"", IF([@[Closure Date]]<>"","Closed",IF([@[Date Repairs Completed]]<>"", "Pending",IF(TODAY()>[@[Target Date]],"Overdue","Open"))))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A10:U19Expression=$A10=0textYES
A10:U19Expression=$U10="Open"textNO
A10:U19Expression=$U10="Closed"textNO
A10:U19Expression=$U10="Pending"textNO
A10:U19Expression=$U10="Overdue"textNO
Cells with Data Validation
CellAllowCriteria
K10:K19List=$E$3:$E$5
L10:L19List=$J$3:$J$5


Code Tagged. This is all the code relating to this function. Let me know if you need more,

VBA Code:
Private Sub btnDone_Click()

Sheet1.Activate

'Email new EMR
Dim objOutlook As Object

Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object



'Create array of email addresses
Dim checkrange As Range

Select Case cbSeverity.Value
   Case "Critical"
   Set checkrange = Range("C2", Range("C2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
   Case "Major"
   Set checkrange = Range("D2", Range("D2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
   Case "Minor"
   Set checkrange = Range("E2", Range("E2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
End Select


Set objOutlook = Nothing
 
Upvote 0
cbSeverity.Value
OK, but you are not telling the code what that value is !!
What cell does it refer to in your table ??
AND
Shouldn't the Case be "Critical EMR","Major EMR","Minor EMR"
 
Upvote 0
cbSeverity.Value
OK, but you are not telling the code what that value is !!
What cell does it refer to in your table ??
AND
Shouldn't the Case be "Critical EMR","Major EMR","Minor EMR"
Regarding the last comment. If I need to change it to "Critical EMR" then why does it work with "Critical"?
 
Upvote 0
First things first...Is cbSeverity a Named Range ??
If so what range does it refer to ??
 
Upvote 0
Can you upload the workbook to a hosting site, Dropbox for instance, and I'll take a closer look.
I think a checkbox, comboBox or dropdown is being selected to trigger the cbseverity.value
 
Upvote 0
This is the New data Entry Button we are using, this lets us enter what issues we are having with what machine, and emails the specific person depending on Severity via the dropdown. Im so sorry i shouldve stated this earlier. Its been a long night/morning.
 

Attachments

  • 1631067705287.png
    1631067705287.png
    39 KB · Views: 6
Upvote 0
This is the Code related to the Data Entry Prompt.

VBA Code:
Dim lastEMR, newEMREnt, newEMRval

Private Sub cbCategory_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = 0
End Sub

Private Sub cbPriority_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = 0
End Sub

Private Sub cbSeverity_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = 0
End Sub

Private Sub Label11_Click()

End Sub

Private Sub lblEMR_Click()

End Sub

Private Sub lblName_Click()

End Sub

Private Sub UserForm_Initialize()
    'Add items to department list
    cbDepartment.AddItem "Engineering"
    cbDepartment.AddItem "Production"
    cbDepartment.AddItem "QA"
    
    'Add items to category list
    cbCategory.AddItem "Equipment"
    cbCategory.AddItem "Utilities"
    cbCategory.AddItem "Building"
    cbCategory.AddItem "HVAC"
    
    'Add items to severity
    cbSeverity.AddItem "Critical"
    cbSeverity.AddItem "Major"
    cbSeverity.AddItem "Minor"
    
    'Add items to priority
    cbPriority.AddItem "1"
    cbPriority.AddItem "2"
    cbPriority.AddItem "3"
    
    With NewEMR.entTargetDate
    .Text = "dd/mm/yy"
    .ForeColor = RGB(204, 204, 204)
    .Font.Size = 8
    
    lastEMR = Range("A10").End(xlDown).Value
    newEMRval = CInt(Right(lastEMR, 3)) + 1
    newEMREnt = Format(Date, "yy") & Format(newEMRval, "-000")
    lblEMR.Caption = newEMREnt
End With
    
End Sub

Private Sub btnCancel_Click()
    Unload Me
End Sub

Private Sub btnDone_Click()

Sheet1.Activate

'Email new EMR
Dim objOutlook As Object

Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object



'Create array of email addresses
Dim checkrange As Range

Select Case cbSeverity.Value
   Case "Critical"
   Set checkrange = Range("C2", Range("C2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
   Case "Major"
   Set checkrange = Range("D2", Range("D2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
   Case "Minor"
   Set checkrange = Range("E2", Range("E2").End(xlDown))
       For Each cell In checkrange
           If cell = "Yes" Then
           Set objEmail = objOutlook.CreateItem(0)
           With objEmail
               .to = cell.Offset(0, -1).Value
               .Subject = "New Equipment Maintanence Request - " & cbSeverity.Value
               .body = "New equipment maintanence request from " & entName.Value & " on " & Date & ", EMR: " & newEMREnt & vbNewLine _
                       & "Category: " & cbCategory.Value & ", Equipment: " & entEquipment.Value & vbNewLine _
                       & "Location: " & entLocation & vbNewLine _
                       & "Effected material code/ product code/ workorder: " & entCode.Value & vbNewLine _
                       & "Description: " & entDescription.Value & vbNewLine _
                       & "Severity: " & cbSeverity.Value & vbNewLine _
                       & "Priority: " & cbPriority.Value & vbNewLine _
                       & "Target Date: " & entTargetDate.Value
               .Send
               Set objEmail = Nothing
           End With
           End If
       Next cell
End Select


Set objOutlook = Nothing

'Log to EMR log
Dim emptyRow
Sheet2.Activate

'Check all entrys populated on user form and target date is valid
If Not IsDate(entTargetDate.Text) Then
    MsgBox """Target Date is not Valid, Please Try Again"""
ElseIf cbCategory.Value = "" Or entEquipment.Value = "" Or entName.Value = "" Or cbDepartment.Value = "" Or entLocation.Value = "" Or entDescription.Value = "" Or cbSeverity.Value = "" Or cbPriority.Value = "" Then
    MsgBox """One or More Enters are Invalid"""
Else
'If all valid
'   vvv
'Insert new emr into new row with year before it
    Range("A10").End(xlDown).Offset(1, 0).Value = newEMREnt
    'Populate table
    Range("A10").End(xlDown).Offset(0, 1).Value = Date
    Range("A10").End(xlDown).Offset(0, 2).Value = cbCategory.Value
    Range("A10").End(xlDown).Offset(0, 3).Value = entEquipment.Value
    Range("A10").End(xlDown).Offset(0, 4).Value = entName.Value
    Range("A10").End(xlDown).Offset(0, 5).Value = cbDepartment.Value
    Range("A10").End(xlDown).Offset(0, 6).Value = entLocation.Value
    Range("A10").End(xlDown).Offset(0, 7).Value = entCode.Value
    Range("A10").End(xlDown).Offset(0, 8).Value = entDescription.Value
    
    If cbFilesAttached = True Then
        Range("A10").End(xlDown).Offset(0, 9).Value = Yes
    Else
        Range("A10").End(xlDown).Offset(0, 9).Value = No
    End If
    
    Range("A10").End(xlDown).Offset(0, 10).Value = cbSeverity.Value
    Range("A10").End(xlDown).Offset(0, 11).Value = cbPriority.Value
    Range("A10").End(xlDown).Offset(0, 12).Value = entTargetDate.Value
'Close user form
Unload Me
End If
    
End Sub

Private Sub entTargetDate_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With NewEMR.entTargetDate
    .Text = ""
    .ForeColor = RGB(0, 0, 51)
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top