Consulting

Results 1 to 4 of 4

Thread: mail range

  1. #1

    mail range

    I'm trying to copy the data in range B5:Q9 directly into an email, if i physically copy and past it into outlook, it takes the colors and format and everything, that what I'm trying to achieve with this button, any ideas? Thanks in advance!
    Private Sub Email_WB_Click()
    RSet WBRange = Range("B5:Q9").Copy
    Set myOlApp = CreateObject("Outlook.Application")
    Set myItem = myOlApp.createitem(olmailitem)
    With myItem
    .Recipients.Add "donald@company.com"
    .Body = WBRange
    .Subject = "Employee Time Accurals"
    End With
    myItem.Send
    End Sub
    Last edited by mdmackillop; 03-14-2006 at 03:53 PM. Reason: Email address replaced

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    This is one way of doing it.
    Add this code as a module :
    [vba]
    Public Function SheetToHTML(sh As Worksheet)

    Dim TempFile As String
    Dim fso As Object
    Dim ts As Object
    Randomize
    sh.Copy
    TempFile = sh.Parent.Path & "\TmpHTML" & Int(Rnd() * 10) & ".htm"
    ActiveWorkbook.SaveAs TempFile, xlHtml
    ActiveWorkbook.Close False
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    SheetToHTML = ts.ReadAll
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
    Kill TempFile

    End Function
    [/vba]

    Then in your code u have listed there, just add :
    [vba]
    .HTMLBody = sheetToHTML(thisworkbook.sheets(3))
    .display
    [/vba]

    Let me know if that works. My Outlook code varies slightly, but works great, w/ links, color, etc.
    PS - mine is on sheet 3 , but change that to what page your is that your copying.

  3. #3
    Thanks for your help!!!

    I tried that and I'm getting an error:

    Compile Error:
    Invalid or unqualified reference

    On:
    .HTMLBody = SheetToHTML(ThisWorkbook.Sheets(1))
    on the sheet the name is actually Total, I tried that and 1 and neither worked.

    Thanks again!!

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    Can you attach the file you are using, or a mock file? Also, did u add the oulook reference ?

    Also, not sure how this differs, but this is the code I use to send it...might be sublte differences.

    [VBA]
    Sub SheetInBody() Ole Function for each employeee
    Dim olApp As Outlook.Application
    Dim olMail As MailItem
    Set olApp = New Outlook.Application
    Set olMail = olApp.CreateItem(olMailItem)
    With olMail
    .To = "person@email.com"
    .Subject = "SLA Service Impacting Delays"
    .HTMLBody = SheetToHTML(ThisWorkbook.Sheets(3))
    .Display
    End With
    ' Set olMail = Nothing
    ' Set olApp = Nothing
    'End Sub
    [/VBA]

Posting Permissions

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