Code to automatically send an email a week before date with a message.

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I have a sheet as set out below. Is there a way or a code that will automatically send an email a week before the date in column B with a message of my choice?

Thanks.

Excel 2010
AB
john@gmail.com
eric@hotmail.com
daz@hotmail.co.uk

<colgroup><col style="width: 25pxpx" https:="" www.mrexcel.com="" forum="" usertag.php?do="list&action=hash&hash=DAE7F5"" target="_blank"></colgroup><colgroup><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]2[/TD]

[TD="align: right"]25/07/2019[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]15/08/2019[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]10/10/2019[/TD]

</tbody>
Sheet1
 
Last edited:
Still spinning and not doing anything. I put ' in front of display and removed ' from in front of send?
 
Last edited:
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
.
On some computers, Outlook needs to be open and running prior to sending the email. If, after you click the SEND button, Outlook doesn't appear as running in
your desktop tray, that would be the indication.

Another thing is to test sending an email via Outlook directly without using the Excel macro. Make certain Outlook is actually connected to your Gmail account and
functioning as required.
 
Upvote 0
.
This is one of several macros contained within the workbook download that allows you to send email directly via Gmail :

Code:
Option Explicit


'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code


'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"


'Use your own mail address to test the code in this line
'.To = "Mail address receiver"


'Change YourName to the From name you want to use
'.From = """YourName"" <Reply@something.nl>"


'If you get this error : The transport failed to connect to the server
'then try to change the SMTP port from 25 to 465


'Possible that you must also enable the "Less Secure" option for GMail
'https://www.google.com/settings/security/lesssecureapps


Sub CDO_Mail_Small_Text_2()
    Dim iMsg As Object
    Dim iConf As Object
    Dim strbody As String
    Dim Flds As Variant
    
    Dim r As Range          ''**************************************************
    Set r = Worksheets("Sheet1").Range("F1:F59").SpecialCells(xlCellTypeVisible)  '**************************************
    
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")


    iConf.Load -1    ' CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"


        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With


    strbody = "Hi there" & vbNewLine & vbNewLine & _
        "This is line 1" & vbNewLine & _
        "This is line 2" & vbNewLine & _
        "This is line 3" & vbNewLine & _
        "This is line 4"


    With iMsg
        Set .Configuration = iConf
        .To = "Mail address receiver"
        .CC = ""
        .BCC = ""
        ' Note: The reply address is not working if you use this Gmail example
        ' It will use your Gmail address automatic. But you can add this line
        ' to change the reply address  .ReplyTo = "Reply@something.nl"
        .From = """YourName"" <Reply@something.nl>"
        .Subject = "Important message"
        
        .HTMLBody = RangetoHTML(r) '****************************************************************************
        
        '.Addattachment "c:\temp\Scripty.zip"    ' <--- edit path to file
        .Send
        
    End With
    
    Set iMsg = Nothing
    Set iConf = Nothing
    Set Flds = Nothing
End Sub
'*************************************************************
'INCLUDE THE ENTIRE FUNCTION BELOW THE EMAIL MACRO THAT IS ABOVE
'*************************************************************
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook


    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile


    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


'INCLUDE THE FUNCTION ABOVE
'*******************************************************************************

Download workbook : https://www.amazon.com/clouddrive/share/sSopQS696TV9pukFmzUwK67gVmh6KTtPCk0WfQuOoz2
 
Upvote 0
.
On some computers, Outlook needs to be open and running prior to sending the email.

That done the trick, opened and minimised it and worked perfect. Thanks for your time. I don't know what I was expecting though because I am still having to do a lot of this manually. i.e open the file daily and press send. Eventually there will be thousands of emails and was looking for something more automated?
 
Upvote 0
.
The sample workbook includes a macro in the ThisWorkbook module that automatically runs the email macro, when the
workbook is opened.

You can include another macro, that will open Outlook prior to running the email macro. Might want to place a small
pause between the two macros to allow Outlook to completely open and run first.
 
Upvote 0
Would you be able to provide that macro please. Would it need to know the dates within so it knows when to open?
 
Upvote 0
.
You will need to manually open the workbook each day. Other programmers have utilized the Windows built-in Scheduler to run a workbook .. so that is an option.
My personal preference is to manually start the workbook so I know for certain it occurred. Then I also prefer to check the process to verify it has been completed
as it should.

This macro should start Outlook :

Code:
Option Explicit


Sub Is_Outlook_Running_Open_App()
    'Declare Variables to Check Get Instance of Outlook Object
    Dim objOutlook As Object
    
    'Initialize
    Set objOutlook = Nothing
    
    'Get Instance of Object
    'Getobject will give error if it did not find the app.
    'Sp, On Error is required all the time
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    
    'Check if Outlook is Running
    If objOutlook Is Nothing Then
        'Outlook is not Running - Open Outlook App
        VBA.Shell ("Outlook")
    Else
        MsgBox "Outlook is already Running in Your Machine"
    End If
End Sub



This line of code will pause Excel for whatever time segment you input in the code :

Code:
Application.Wait (Now + TimeValue("0:00:01")) '<-- "0:00:01" = Hrs:Minutes:Seconds .... "0:00:05" = five seconds
 
Upvote 0

Forum statistics

Threads
1,223,681
Messages
6,173,815
Members
452,535
Latest member
berdex

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