+ Reply to Thread
Results 1 to 6 of 6

Send an excelsheet using vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    882

    Send an excelsheet using vba

    Hi guys

    I am writing the following code to send excelsheet through email but it doesn't work.

     ReportDateAndTime = Now
    Call Mail_Selection_Range_Outlook_Body("Report")
    
    Sub Mail_Selection_Range_Outlook_Body(a As String)
    
        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set rng = Nothing
        On Error Resume Next
       
        Set rng = ThisWorkbook.Worksheets("BaNCS Cheque Report").UsedRange
        
        On Error GoTo 0
    
        
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set OutApp = New Outlook.Application
        Set OutMail = OutApp.CreateItem(olMailItem)
    
        On Error Resume Next
        With OutMail
         
                   .Recipients.Add "amanpreet.kaur@axa-sunlife.co.uk"
                    .Recipients.Add "mark.bickers@williamslea.com"
                 
                    
            .Subject = a
            .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

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,605

    Re: Send an excelsheet using vba

    Perhaps you will want to have a look at Ron de Bruin's solution
    The mail add-in he built is very easy to use, and the code is available if needed

  3. #3
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Send an excelsheet using vba

    You need to copy the function rangetoHTML also.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  4. #4
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    882

    Re: Send an excelsheet using vba

    I copied the following code but still doesn't send an email.
    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, "dd-mm-yy h-mm-ss") & ".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
     
        '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

  5. #5
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    882

    Re: Send an excelsheet using vba

    The above code works fine for excel 2003 but why not for excel 2007.

    Please help me out in this.

    Thanks

  6. #6
    Forum Contributor
    Join Date
    09-24-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Send an excelsheet using vba

    maybe try this..
    Sub Mail_Workbook_1()
    ' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010.
    ' This example sends the last saved version of the Activeworkbook object .
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
       ' Change the mail address and subject in the macro before you run it.
        With OutMail
            .To = "name@whatever.com"  '(email address goes here seperated by a ;)
    	.CC = "name@whatever.com"  '(email address goes here seperated by a ;)
    	.BCC = "name@whatever.com"  '(email address goes here seperated by a ;)
    	.Subject = "Hi my name is." '(subject goes here)
            .Body = "See Attached"
            '.Attachments.Add ActiveWorkbook.FullName
            ' You can add other files by uncommenting the following line.
            .Attachments.Add ("C:\Documents and Settings\Desktop\Filename.pdf") 'Input path to file.
            ' In place of the following statement, you can use ".Display" to
            ' display the Email.
            .Send   
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Send outlook task from Excel sheet (including the excelsheet)
    By Jop_4444 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-08-2012, 05:26 PM
  2. Excel 2007 : Excelsheet checklist
    By Chainsaw_019 in forum Excel General
    Replies: 2
    Last Post: 07-25-2011, 07:26 AM
  3. Send excelsheet in the body of an email using vba
    By Nancy123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-30-2010, 07:06 AM
  4. [SOLVED] How to use a macro from another excelsheet
    By Jeroen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-24-2006, 10:25 AM
  5. Protected Excelsheet
    By ngmahesh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2006, 09:31 AM

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