+ Reply to Thread
Results 1 to 2 of 2

VBA pull data from sheet map to outlook body msg

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-21-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007,2010,2016
    Posts
    695

    VBA pull data from sheet map to outlook body msg

    i have assigned code to send mail through excel in outlook but

    but i want assign the body text as sheet range(A3:I10) of pivot this range map to bodytext of every mail from every workbooks.

    i have enclosed two files one is mailsender and another sheet which to attach and send contains PIVOT sheet in first tab.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    06-04-2009
    Location
    Stourbridge
    MS-Off Ver
    Excel 2013
    Posts
    26

    Re: VBA pull data from sheet map to outlook body msg

    Hi,

    you need to use the RangetoHtml function from RonDeBruin to paste the required range into the msg body.
    http://www.rondebruin.nl/win/s1/outlook/bmail6.htm

    The Aegis Inforware book needs to be opened to copy the range.
    ....I forgot you'll need to add code to close the opened file once the mail has been sent!!

    You may have to modify the code but hopefully you'll get the gist.

    
    Subj = Cells(I, "A").Value
    Filepath = Cells(I, "B").Value 
    EmailTo = Cells(I, "C").Value
    CCto = Cells(I, "D").Value
    msg = Cells(I, "E").Value
    
    'Open relevant file from Column B
    Workbooks.Open Filepath 
    'Get the filename from the path
    strFile = Right(Filepath, Len(Filepath) - InStrRev(Filepath, "\"))
    'Activate the file
    Windows(strFile).Activate
    'Get the range 
    Set Rng = ActiveSheet.Range("A3:I10")
    
    Application.DisplayAlerts = False
    
    ..........................
    
    Then use HTMLBody and function RangetoHTML instead of .body
    
    .HTMLBody = RangetoHTML(rng)
    ' .body = msg
    
    
    'Add RangetoHTML function
    Function RangetoHTML(rng As Range)
    ' Changed by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2010
        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
    Last edited by daverunt; 06-07-2013 at 12:42 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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