Autorun VBA, send specific values to Outlook

Elvin A

New Member
Joined
Jun 2, 2023
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Good day everyone.

I am trying to make this code run so every time I open it so excel automatically checks for expired equipment, put message about it and then send it to Outlook.
Ideally i want all outdated equipment to be listed in email too.
For example if equipment flagged already as "ALERT!", excel upon openning file just do not do anything

i tried to copy the code to Workbook section and insert it there but it is not working.
VBA Code:
Private Sub Workbook_Open()

' I put my code there

End Sub

VBA Code:
Dim exRg As String
Dim xRg As Range

'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
  Set xRg = Intersect(Range("E2:E10000"), Target)
    If xRg Is Nothing Then Exit Sub
exRg = Target.Offset(0, -4)
    If IsNumeric(Target.Value) And Target.Value < 14 And Target.Value > 0 Then
        MsgBox exRg & " is Expired"
        
        Call Mail_small_Text_Outlook
    End If
End Sub
Sub Mail_small_Text_Outlook()
    'Dim xWorkSheet As Worksheet
 
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Dear All," & "<br/><br/>" & "This is Autogenerated email. Please note " & exRg & " soon will be expired <br/><br/>" & "Regards,"
    
       
    On Error Resume Next
    With xOutMail
        .to = ""
        .CC = ""
        .BCC = ""
        .Subject = "[AUTO] " & exRg & " expiration"
        .HTMLBody = xMailBody
        .Display   'or use .Send
        
        'MsgBox xOutMail.Subject 
        
        
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Equipment Calibration Email Send Test - Copy.xlsm
ABCDEF
1EquipmentCert dateExpirationTODAY DateDays leftStatus
2Equipment N129.06.202229.06.202302.06.202327Warning
3Equipment N213.06.202213.06.202302.06.202311ALERT!
4Equipment N314.06.202214.06.202302.06.202312ALERT!
5Equipment N415.06.202215.06.202302.06.202313ALERT!
6Equipment N534GOOD
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=B2+365
D2:D5D2=TODAY()
E2:E5E2=C2-D2
F2:F6F2=IF(E2<14,"ALERT!",IF(AND(E2>=14,E2<30),"Warning","GOOD"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E:EOther TypeIcon setNO
F:FCell Valuecontains "ALERT!"textNO
F:FCell Valuecontains "Warning"textNO
F:FCell Valuecontains "GOOD"textNO


your help is appreciated
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
the reason i added exrg variable I could not indicate it in outlook mail body. that was way around, i believe it can be replaced with offset of xrg variable but i could not figure it out
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Auto update VBA on opening. send to Outlook
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Auto update VBA on opening. send to Outlook
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
noted, thanks, though i could post any links or any attachements in the other excel forum, so i did not do that here too.

any ideas related to the matter?
 
Upvote 0
though i could post any links or any attachements in the other excel forum, so i did not do that here too.

any ideas related to the matter?
Did you mean that you could not post attachments here?
If so, there is a 'Guidelines' link in my signature block below. Follow that and look specifically at section B.
 
Upvote 0
Did you mean that you could not post attachments here?
If so, there is a 'Guidelines' link in my signature block below. Follow that and look specifically at section B.
I do not know if it visible for anybody else but I managed to add minisheet via excell addin here.
Do not know if i can attach excel file itself though.
p.s. can i edit my posts here?
 
Upvote 0
I do not know if it visible for anybody else but I managed to add minisheet via excell addin here.
Yes, it is visible, your mini sheet is fine. (y)

Do not know if i can attach excel file itself though.
If you followed the link I suggested above and looked at point 1 of the section B that I referred you to, you would know the answer to that. ;)

p.s. can i edit my posts here?
When you first join you cannot (it is an anti-spam measure). However, very soon (after more posts) you should see an edit link under your new posts. You will then have 10 minutes in which time you will be able to edit the post.
 
Upvote 0
k1dr0ck helped me to do it
 
Upvote 0
Solution

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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