Results 1 to 3 of 3

VBA to select a Range of cells as Email Body In Outlook

Threaded View

  1. #1
    Registered User
    Join Date
    09-12-2013
    Location
    Midlands, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    VBA to select a Range of cells as Email Body In Outlook

    I really could do with some help.

    I am trying to send the info from cells A1:G38 as the body of my email.

    I need two options to send the same data one for a day shift and one for a night shift this is due to the automated subject line.

    The code sends the email just the body of the email is always blank any ideas? Also when I add the second function (Night shift) using the same code as I used for the (day shift) the macro doesn't run at all?

    I am new to VBA but enjoying the challenge.

    Thank you in advance.

    This is the code I am using, and I have attached the excel spreadsheet.

    Sub CommandButton99_Click()
    
    Today = Format(Now(), "[$-F800] dddd, mmmm dd, yyyy")
     
        Dim rng As RANGE
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set rng = Nothing
        On Error Resume Next
        
        
      Set rng = Sheets("Sheet1").RANGE("A1:G38").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")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = "XYZ@XYZ.com"
            .CC = ""
            .BCC = ""
            .subject = "PLC SUPPORT SHIFT REPORT Days" & Today
            .HTMLBody = RangetoHTML(rng)
            .Send
        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)
    
        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook
    
        TempFile = Environ$("temp") & "\" & Format(Now(), "[$-F800]dddd, mmmm dd, yyyy").HTM
    
            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
    
       
        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
    
       
        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=")
    
        
        TempWB.Close savechanges:=False
    
       
        Kill TempFile
    
        Set ts = Nothing
        Set fso = Nothing
        Set TempWB = Nothing
    End Function
    PLC Shift report.xlsm
    Last edited by TC922; 09-16-2013 at 07:36 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Find the range of a table & copy/past into body of an Outlook email
    By JamesGilchrist in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2014, 08:44 PM
  2. [SOLVED] VBA Code to select Range of cells as Email Body In Outlook
    By naveenmarapaka in forum Outlook Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2013, 06:43 AM
  3. export outlook 2007 email into excel with subject and body of email
    By akulka58 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2013, 02:37 PM
  4. Copy/paste select cells in active row for use in email body
    By rkovsonic in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-22-2013, 05:41 PM
  5. Replies: 2
    Last Post: 08-01-2012, 02:47 PM

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.6.0 RC 1