Auto E-Mail and Scheduler from Excel

I have an existing spreadsheet that pulls data from a SQL Server displays it in 4 Columns within Excel:
Symbol      LastPrice        NetChg        PerfRating
ABC           23.25             .50%               1
RTY           19.88             -2.68%            4
XYZ           7.12              .23%                2
UYT           89.45             -.10                  3

Since we our user base uses Blackberry's to view this data, I currently use a some basic VBA Macros + VB SendKey commands to copy this information to Notepad and then to the clipboard.  Once it's in Clipboard (memory), the senders can paste into their e-mail and send to the intended recipients manually.  Why would I past to Notepad you say?.....Since we use Lotus Notes, it was the only way I could retain the column formatting so the information displayed in readable columns on the Blackberry (we don't have the option of using monospace fonts on the Blackberry).

I am hoping to relieve our user from manually updating th query, pasting into Notes and manually sending out.  The perfect solution would be they open the spreadsheet, and it sends out the update every hour to a pre-defined set of receipients, on the hour (10:00am, 11:00am, Noon...till 5:00pm).  I understand this would be easier if we were just sending an attachment, but I need to retain text formatting.  Allso... we have to send in text, so user can immeadiatly see information without opening attachments.

Some of our internal developers are telling me that this is not possible, within VBA/Excel/VB and will require a new .Net application/utility.  I want a second opinion before I'm proved wrong.

Any ideas?
maverickcapitalAsked:
Who is Participating?
 
patrickabConnect With a Mentor Commented:
maverickcapital,

Using code written by Ron de Bruin - see this link:

http://www.rondebruin.nl/mail/folder3/mail4.htm

It's easy to send in the body of an email a range of data which has been converted to HTML - hopefully you can see the result in the attached .bmp. It even coped with Conditionally Formatted cells and cell outlines.

It's all in the attached Excel file.

I hope it's of help.

Patrick
Sub Mail_Selection_Range_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
 
    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Sheets("Sheet1").Range("A1:G22")
    'Selection.SpecialCells (xlCellTypeVisible)
    'You can also use a range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
 
    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If
 
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
 
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
 
    On Error Resume Next
    With OutMail
        .To = "Your email address here"
        .CC = ""
        .BCC = ""
        .Subject = "Test of embedded data"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
 
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
    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

Open in new window

email-test-embedded-data-01.xls
embedded-data-01.bmp
0
 
patrickabCommented:
I should have explained that the .bmp (in embedded-data-01.bmp) is only a screen shot of the body of the email I sent to myself using the above code.
0
 
patrickabCommented:
maverickcapital,

Several points:

1. If the result is OK then it is a relatively easy matter to have a macro that runs this every hour.

2. The range can be hard-coded or it can be dynamic or indeed you could define it at run-time via a dialogue box.

3. The list of recipients can be defined and hard-coded, or you can make it work through a list of them on say another worksheet.

Patrick
0
 
maverickcapitalAuthor Commented:
Thanks for thr response....I will take a look at the above code this week, and let you know if there are any problems/questions.
-John
0
 
patrickabCommented:
John,

May I suggest that you first just test it by editing this line of code:

.To = "Your email address here"

to something like this:

.To = "john.smith@maverickcapital.com"

and then press the button to send the data to your or a guinea pig's Blackberry.

Patrick
0
All Courses

From novice to tech pro — start learning today.