+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    03-06-2010
    Location
    essex, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    email range based on date value

    Hi all,
    Im having trouble with a worksheet that i am running,

    the worksheet data is:
    B3: ACTIONS C3: DATE D3: COMMENTS
    B4: TASK1 C4: 25/1/10 D4: RANDOM COMMENT HERE
    B5: TASK2 C5: 4/3/10 D5: RANDOM COMMENT HERE
    B6: TASK3 C6: 3/3/10 D6: RANDOM COMMENT HERE
    B7: TASK4 C7: 19/5/10 D7: RANDOM COMMENT HERE

    and so on untill about row 200 or so

    what i need is:

    if c4 > todays date + 10 then email cells b4, c4 and d4 (in the body of the email) to whoever@something.com
    if c5 > todays date + 10 then email cells b5, c5 and d5 (in the body of the email) to whoever@something.com

    I would prefer if the emails could be seperate rather than all tasks being on one email, but it doesnt matter

    i am using this for a kinda work to list so for example if out of the whole sheet there was only 2 dates due i would recieve 2 emails and could work through them, when they are finished i would delete the emails then delete them from the excel sheet

    I know its a kinda funny request but it would make my life a lot easier.

    Also if anyone does come up with anything could you please explain what the different parts of the macro are doing so i can learn from it and possibly help others in the future.

    P.s i use outlook express 6

    thanks for your help
    Last edited by '''your code here; 03-20-2010 at 01:20 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,495

    Re: email range based on date value

    Hello ''your code here,

    This was quite a challenge. The macro will read the data on the worksheet, combine the cell information and then email it provided the date is greater than 10 days from today. Once the email is displayed, you will need to click Send or Cancel. This process will be repeated until there are no more emails to be sent.

    What makes this tricky is allowing the user to manually send the email fro Outlook Express and return back to Excel. Seems like it should be easy. This code will show you that there is a lot more "under the hood" to make this happen. The real work is done by the API (Application Programming Interface) code.

    In the macro SendEmails you will need to change the recipient's email address (SendTo) and the subject line (Subject) to what you want to use. If you want to send the emails to more than one recipient, let me know.

    [bPlace this Code in a Separate VBA Module[/b]
    Code:
    'Written: March 17, 2010
    'Author:  Leith Ross
    'Summary: Start Outlook Express and send emails from a worksheet.
    
     Private Const NORMAL_PRIORITY_CLASS = &H20&
     Private Const WAIT_INFINITE = -1&
     Private Const SYNCHRONIZE = &H100000
    
     Private Type STARTUPINFO
          cb As Long
          lpReserved As String
          lpDesktop As String
          lpTitle As String
          dwX As Long
          dwY As Long
          dwXSize As Long
          dwYSize As Long
          dwXCountChars As Long
          dwYCountChars As Long
          dwFillAttribute As Long
          dwFlags As Long
          wShowWindow As Integer
          cbReserved2 As Integer
          lpReserved2 As Long
          hStdInput As Long
          hStdOutput As Long
          hStdError As Long
     End Type
    
     Private Type PROCESS_INFORMATION
          hProcess As Long
          hThread As Long
          dwProcessId As Long
          dwThreadID As Long
     End Type
    
     Private Declare Function CreateProcess _
       Lib "kernel32" _
         Alias "CreateProcessA" _
           (ByVal lpApplicationName As String, _
            ByVal lpCommandLine As String, _
            ByVal lpProcessAttributes As Long, _
            ByVal lpThreadAttributes As Long, _
            ByVal bInheritHandles As Long, _
            ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, _
            ByVal lpCurrentDirectory As String, _
            ByRef lpStartupInfo As STARTUPINFO, _
            ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
    
     Private Declare Function WaitForSingleObject _
       Lib "kernel32.dll" _
         (ByVal hHandle As Long, _
          ByVal dwMilliseconds As Long) As Long
          
     Private Declare Function CloseHandle _
       Lib "kernel32" _
         (ByVal hObject As Long) As Long
      
    'This converts long file names to short names with no spaces
     Private Declare Function GetShortPathName _
       Lib "kernel32.dll" _
         Alias "GetShortPathNameA" _
           (ByVal longPath As String, _
            ByVal shortPath As String, _
            ByVal shortBufferSize As Long) As Long
     
    Sub SendEmailFromOE(ByVal SendTo As String, ByVal Subject As String, ByVal Msg As String)
    
      Dim cmdline As String
      Dim PI As PROCESS_INFORMATION
      Dim progPath As String
      Dim shortBuffer As String
      Dim SI As STARTUPINFO
      Dim StartPath As String
      Dim RetVal As Long
      
        'Get path to Outlook Express
         App = Environ("ProgramFiles") & "\Outlook Express\msimn.exe"
        
        'Setup the command line arguments
         cmdline = " /mailurl:MailTo:" & SendTo & "?Subject=" & Subject & "&Body=" & Msg
             
        'Set the length of the StartupInfo block
         SI.cb = Len(SI)
         
        'Start the shelled application:
         RetVal = CreateProcess(lpApplicationName:=App, _
                                lpCommandLine:=cmdline, _
                                lpProcessAttributes:=0, _
                                lpThreadAttributes:=0, _
                                bInheritHandles:=1, _
                                dwCreationFlags:=NORMAL_PRIORITY_CLASS, _
                                lpEnvironment:=0, _
                                lpCurrentDirectory:=StartPath, _
                                lpStartupInfo:=SI, _
                                lpProcessInformation:=PI)
    
        'Wait for Outlook Express to close
         RetVal = WaitForSingleObject(PI.hProcess, WAIT_INFINITE)
         
        'Free the handle to be reused
         CloseHandle PI.hProcess
         
    End Sub
    
    Sub SendEmails()
    
     'Send emails and allow user to send them manually one at a time.
     
      Dim Cell As Range
      Dim DueDate As Date
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        SendTo = "towhomever@something.com"  '<<<<< Change Recipient Email Address
        Subject = "Subject line text"        '<<<<< Change Subject Line
    
        Set Wks = Worksheets("Sheet1")
        
       'Dynamically size the range
        Set Rng = Wks.Range("B4")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'Exit if there is no data
        If RngEnd.Row < Rng.Row Then Exit Sub
        
       'Cells B3:D3 to Bx:Dx
        Set Rng = Wks.Range(Rng, RngEnd).Resize(ColumnSize:=3)
          
          DueDate = Int(Now()) + 10
          
         'Check dates and send email
          For Each Cell In Rng.Columns(2).Cells
            If Cell > DueDate Then
               Msg = Cell.Offset(0, -1) & " " & Cell & " " & Cell.Offset(0, 1)
               SendEmailFromOE SendTo, Subject, Msg
            End If
          Next Cell
          
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-06-2010
    Location
    essex, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: email range based on date value

    wow, firstly thanks for your input, that must have taken a long time and i appreciate the effort you have put in.
    now this may sound a bit cheeky but would it be possible to bypass the part where you have to send the mail manually, so it will send the mail without the outlook screen popping up, so when i click the button it will do it all in the backround.

    thanks again

    ps i changed a tiny piece of the code, my fault i got the > sign round the wrong way

    Code:
    Sub SendEmails()
    
     'Send emails and allow user to send them manually one at a time.
     
      Dim Cell As Range
      Dim DueDate As Date
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        SendTo = "address here"  '<<<<< Change Recipient Email Address
        Subject = "subject here"        '<<<<< Change Subject Line
    
        Set Wks = Worksheets("Sheet1")
        
       'Dynamically size the range
        Set Rng = Wks.Range("B4")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'Exit if there is no data
        If RngEnd.Row < Rng.Row Then Exit Sub
        
       'Cells B3:D3 to Bx:Dx
        Set Rng = Wks.Range(Rng, RngEnd).Resize(ColumnSize:=3)
          
          
         'Check dates and send email
          For Each Cell In Rng.Columns(2).Cells
            If Cell < Date + 10 Then
               Msg = Cell.Offset(0, -1) & " " & Cell & " " & Cell.Offset(0, 1)
               SendEmailFromOE SendTo, Subject, Msg
            End If
          Next Cell
          
    End Sub

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,495

    Re: email range based on date value

    Hello your code goes here,

    Actually, it didn't take me that long to do. Your question isn't cheeky. The problem with fully automating your email process is that Outlook Express does not support automation. There is no easy way to click the "Send" button nor any VBA code to directly access it. It is very easy do what you want with Outlook because it was designed for automation. Truthfully, if want to automate the process then you need to use Outlook.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    03-06-2010
    Location
    essex, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: email range based on date value

    ok, would it be easy to do for outlook 2003, i have that at work and i would like to use this code there aswell.
    If i wanted to change the range of the selected cell, say i wanted it to search the E colum for the date and copy A4 to say F4 to the email, could you show me which bit of the code i would change to do this, im guessing its proberly something to do with these lines

    Code:
       'Dynamically size the range
        Set Rng = Wks.Range("B4")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'Exit if there is no data
        If RngEnd.Row < Rng.Row Then Exit Sub
        
       'Cells B3:D3 to Bx:Dx
        Set Rng = Wks.Range(Rng, RngEnd).Resize(ColumnSize:=3)
    thanks pete

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,495

    Re: email range based on date value

    Hello Pete,

    I changed the code to search for the dates in column "E" and extract the cells in column "A:F". The cells are separated by spaces. Yo may want to add new line characters to format the mail message the way you want.
    Code:
    Sub SendEmails()
    
     'Send emails and allow user to send them manually one at a time.
     
      Dim Cell As Range
      Dim DueDate As Date
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        SendTo = "towhomever@something.com"  '<<<<< Change Recipient Email Address
        Subject = "Subject line text"        '<<<<< Change Subject Line
    
        Set Wks = Worksheets("Sheet1")
        
       'Get the size the date column
        Set Rng = Wks.Range("E4")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'Exit if there is no data
        If RngEnd.Row < Rng.Row Then Exit Sub
        
       'Cells A4:F4 to Ax:Fx
        Set Rng = Wks.Range(Rng, RngEnd).Offset(0, -4).Resize(ColumnSize:=6)
          
          DueDate = Int(Now()) + 10
          
         'Check dates and send email
          For Each Cell In Rng.Columns(5).Cells
            If Cell > DueDate Then
               Msg = Cell.Offset(0, -4) & " " _
                   & Cell.Offset(0, -3) & " " _
                   & Cell.Offset(0, -2) & " " _
                   & Cell.Offset(0, -1) & " " _
                   & Cell.Offset(0, 0) & " " _
                   & Cell.Offset(0, 1)
               SendEmailFromOE SendTo, Subject, Msg
            End If
          Next Cell
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    02-18-2010
    Location
    Pune
    MS-Off Ver
    Excel 2003
    Posts
    9

    email range based on date value

    Quote Originally Posted by Leith Ross View Post
    Hello Pete,

    I changed the code to search for the dates in column "E" and extract the cells in column "A:F". The cells are separated by spaces. Yo may want to add new line characters to format the mail message the way you want.
    Code:
    Sub SendEmails()
    
     'Send emails and allow user to send them manually one at a time.
     
      Dim Cell As Range
      Dim DueDate As Date
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        SendTo = "towhomever@something.com"  '<<<<< Change Recipient Email Address
        Subject = "Subject line text"        '<<<<< Change Subject Line
    
        Set Wks = Worksheets("Sheet1")
        
       'Get the size the date column
        Set Rng = Wks.Range("E4")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'Exit if there is no data
        If RngEnd.Row < Rng.Row Then Exit Sub
        
       'Cells A4:F4 to Ax:Fx
        Set Rng = Wks.Range(Rng, RngEnd).Offset(0, -4).Resize(ColumnSize:=6)
          
          DueDate = Int(Now()) + 10
          
         'Check dates and send email
          For Each Cell In Rng.Columns(5).Cells
            If Cell > DueDate Then
               Msg = Cell.Offset(0, -4) & " " _
                   & Cell.Offset(0, -3) & " " _
                   & Cell.Offset(0, -2) & " " _
                   & Cell.Offset(0, -1) & " " _
                   & Cell.Offset(0, 0) & " " _
                   & Cell.Offset(0, 1)
               SendEmailFromOE SendTo, Subject, Msg
            End If
          Next Cell
          
    End Sub
    --------------------
    Hi

    It is still going through outlook express. Will it not work with MIcrosoft Outlook ?

    regards
    sanjay
    Last edited by sanjay19961; 03-19-2010 at 09:24 AM.

  8. #8
    Registered User
    Join Date
    03-06-2010
    Location
    essex, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: email range based on date value

    thanks for all the help, the code worked great

  9. #9
    Registered User
    Join Date
    02-18-2010
    Location
    Pune
    MS-Off Ver
    Excel 2003
    Posts
    9

    email range based on date value

    Please can anyone help me in modifying the code here so that I can use it with Microsoft outlook and instead of sending email for individual row, I want a consolidatged report containing all rows where we have past 10 days.

    regards
    sanjay
    ===============================================================
    Sub SendEmails()

    'Send emails and allow user to send them manually one at a time.

    Dim Cell As Range
    Dim DueDate As Date
    Dim Rng As Range
    Dim RngEnd As Range
    Dim Wks As Worksheet

    SendTo = "towhomever@something.com" '<<<<< Change Recipient Email Address
    Subject = "Subject line text" '<<<<< Change Subject Line

    Set Wks = Worksheets("Sheet1")

    'Get the size the date column
    Set Rng = Wks.Range("E4")
    Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)

    'Exit if there is no data
    If RngEnd.Row < Rng.Row Then Exit Sub

    'Cells A4:F4 to Ax:Fx
    Set Rng = Wks.Range(Rng, RngEnd).Offset(0, -4).Resize(ColumnSize:=6)

    DueDate = Int(Now()) + 10

    'Check dates and send email
    For Each Cell In Rng.Columns(5).Cells
    If Cell > DueDate Then
    Msg = Cell.Offset(0, -4) & " " _
    & Cell.Offset(0, -3) & " " _
    & Cell.Offset(0, -2) & " " _
    & Cell.Offset(0, -1) & " " _
    & Cell.Offset(0, 0) & " " _
    & Cell.Offset(0, 1)
    SendEmailFromOE SendTo, Subject, Msg
    End If
    Next Cell

    End Sub

  10. #10
    Registered User
    Join Date
    03-06-2010
    Location
    essex, england
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: email range based on date value

    Hi sanjay,
    I was recommended this website for problems like yours; http://www.rondebruin.nl/sendmail.htm
    There is a lot of useful information on the page and im sure you will find your answer there.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0