Avatar of chris pike
chris pikeFlag for Canada

asked on 

Excel to Outlook for expiry dates (additional Help required)

Hi there I need help with the VB.

I need to be able to add more additional entries at the bottom of the list.
I also would love it if this code could work for each sheet, so i can categorize each type of data per worksheet.

here is the code
Sub RunExpReport()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2013
    Dim rng As Range
    Dim EmailTo As String
    Dim DaysBefore As Long
    Dim TodayLong As Long
    Dim sBody As String
    Dim OutApp As Object
    Dim OutMail As Object

    TodayLong = Now
    DaysBefore = Range("DaysBeforeExp")
    EmailTo = Range("EmailAddress")
    sBody = "Here is the expiration report as of " & Format(Now, "yyyy/mm/dd") & vbNewLine

    For Each rng In Range("ExpDates")
        If TodayLong + DaysBefore >= DateValue(rng) And DateValue(rng) > TodayLong Then
            sBody = sBody & vbNewLine & rng.Offset(, -2) & "    " & rng.Offset(, -1) & "    " & Format(rng, "yyyy/mm/dd")
        End If
    Next rng
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = EmailTo
        .CC = ""
        .BCC = ""
        .Subject = "Expiration Date Report as of " & Format(Now, "yyyy/mm/dd")
        .body = sBody
        '.Send 'use this instead of .display if you want
        .display
    End With

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Here is spreadsheet
Q_28733860-RevA.xlsm
OutlookMicrosoft Excel

Avatar of undefined
Last Comment
chris pike
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

you can try loop through all your worksheets by adding:

For Each ws In ThisWorkbook.Sheets
  '.....
Next

for example, try customize this...

Sub RunExpReport()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2013
    Dim rng As Range
    Dim EmailTo As String
    Dim DaysBefore As Long
    Dim TodayLong As Long
    Dim sBody As String
    Dim OutApp As Object
    Dim OutMail As Object
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
    
        TodayLong = Now
        DaysBefore = ws.Range("DaysBeforeExp")
        EmailTo = ws.Range("EmailAddress")
        sBody = "Here is the expiration report as of " & Format(Now, "yyyy/mm/dd") & vbNewLine
    
        For Each rng In ws.Range("ExpDates")
            If TodayLong + DaysBefore >= DateValue(rng) And DateValue(rng) > TodayLong Then
                sBody = sBody & vbNewLine & rng.Offset(, -2) & "    " & rng.Offset(, -1) & "    " & Format(rng, "yyyy/mm/dd")
            End If
        Next rng
        
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = EmailTo
            .CC = ""
            .BCC = ""
            .Subject = "Expiration Date Report as of " & Format(Now, "yyyy/mm/dd")
            .body = sBody
            '.Send 'use this instead of .display if you want
            .display
        End With
    
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    Next
End Sub

Open in new window

Q_28733860-RevA_b.xlsm
ASKER CERTIFIED SOLUTION
Avatar of kgerb
kgerb
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of chris pike
chris pike
Flag of Canada image

ASKER

Looking now thanks Kyle
Avatar of chris pike
chris pike
Flag of Canada image

ASKER

Kyle,
I lest you a private message, did you get it???
Thanks
Chris
Avatar of chris pike
chris pike
Flag of Canada image

ASKER

Awesome help on this project. Very helpful and quick turn around.
Very much appreciated.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo